Skip to content

fix: improve basePath resolution for npx execution support#116

Merged
QuantGeekDev merged 1 commit intoQuantGeekDev:mainfrom
m2de:fix/basepath-resolution-npx-support
Apr 1, 2026
Merged

fix: improve basePath resolution for npx execution support#116
QuantGeekDev merged 1 commit intoQuantGeekDev:mainfrom
m2de:fix/basepath-resolution-npx-support

Conversation

@m2de
Copy link
Copy Markdown
Contributor

@m2de m2de commented Aug 6, 2025

Problem

Fixes #101 - MCP servers created with mcp-framework fail when executed via npx with the error "Server does not support tools (required for tools/list)".

The issue occurs because the current MCPServer.resolveBasePath() method uses dirname(process.argv[1]) which points to different locations when running via:

  • npm run start (works) ✅
  • npx package-name (fails) ❌

Solution

Updated MCPServer.resolveBasePath() to use the same intelligent resolution strategy as BaseLoader:

  1. Check project dist directory first: process.cwd() + '/dist'
  2. Smart fallback: Use process.argv[1] with intelligent dist/ detection
  3. Consistent behavior: Works with both development and distribution scenarios

Changes Made

  • Added existsSync import from 'fs'
  • Enhanced resolveBasePath() method with multi-stage resolution logic
  • Added debug logging for path resolution tracing
  • Maintains full backward compatibility with explicit basePath configs

Testing

✅ Verified fix works with both execution methods:

  • npm run start - continues to work
  • npx package-name - now works correctly
  • Tools are properly discovered and loaded
  • Server starts successfully with capabilities detected

Benefits

  • 🔧 Fixes the root cause - No more manual basePath workarounds needed
  • 🔄 Backward compatible - Existing projects continue to work unchanged
  • 🎯 Consistent behavior - Same resolution logic as BaseLoader
  • 🚀 Better DX - MCP servers work seamlessly with npx distribution

Copilot AI review requested due to automatic review settings August 6, 2025 13:40
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a critical issue where MCP servers fail when executed via npx due to incorrect basePath resolution. The fix implements a multi-stage resolution strategy that first checks for a project's dist directory, then falls back to intelligent module path detection.

  • Replaces simple dirname(process.argv[1]) with robust multi-stage basePath resolution
  • Adds intelligent detection of dist directories to handle both npm and npx execution contexts
  • Implements consistent resolution logic matching the existing BaseLoader approach

Comment thread src/core/MCPServer.ts
Comment thread src/core/MCPServer.ts
@PaulRBerg
Copy link
Copy Markdown
Contributor

hey @QuantGeekDev could you please take a look at this PR?

Updated MCPServer.resolveBasePath() to use a 3-stage resolution strategy
consistent with BaseLoader: check cwd/dist first, walk up from argv[1]
to find dist/, then fall back to argv[1] dirname. This fixes QuantGeekDev#101 where
npx execution failed because argv[1] pointed to a temp directory.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
@QuantGeekDev QuantGeekDev force-pushed the fix/basepath-resolution-npx-support branch from 7bb68c1 to 6402c82 Compare April 1, 2026 17:20
@QuantGeekDev QuantGeekDev merged commit bff2347 into QuantGeekDev:main Apr 1, 2026
@QuantGeekDev
Copy link
Copy Markdown
Owner

@PaulRBerg Thank you for your contribution, it's neat

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Question] How to run mcp using NPX

4 participants