Skip to content

fix: improve basePath resolution for npx execution support #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

m2de
Copy link

@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

- Add intelligent basePath resolution that checks project dist/ directory first
- Fallback to process.argv[1] with smart dist/ detection
- Ensures MCP servers work with both 'npm run start' and 'npx package-name'
- Maintains backward compatibility with explicit basePath configs
- Consistent with BaseLoader resolution strategy

Fixes QuantGeekDev#101
@Copilot Copilot AI review requested due to automatic review settings August 6, 2025 13:40
Copy link

@Copilot 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

@PaulRBerg
Copy link

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

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
2 participants