Skip to content
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

fix: Add checks and better error messages on server object for mcp run #297

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,8 @@ python server.py
mcp run server.py
```

Note that `mcp run` or `mcp dev` only supports server using FastMCP and not the low-level server variant.

### Mounting to an Existing ASGI Server

You can mount the SSE server to an existing ASGI server using the `sse_app` method. This allows you to integrate the SSE server with other ASGI applications.
Expand Down Expand Up @@ -546,6 +548,8 @@ if __name__ == "__main__":
asyncio.run(run())
```

Caution: The `mcp run` and `mcp dev` tool doesn't support low-level server.

### Writing MCP Clients

The SDK provides a high-level client interface for connecting to MCP servers:
Expand Down
41 changes: 39 additions & 2 deletions src/mcp/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
import subprocess
import sys
from pathlib import Path
from typing import Annotated
from typing import Annotated, Any

from mcp.server import FastMCP
from mcp.server import Server as LowLevelServer

try:
import typer
Expand Down Expand Up @@ -141,17 +144,48 @@ def _import_server(file: Path, server_object: str | None = None):
module = importlib.util.module_from_spec(spec)
spec.loader.exec_module(module)

def _check_server_object(server_object: Any, object_name: str):
"""Helper function to check that the server object is supported

Args:
server_object: The server object to check.

Returns:
True if it's supported.
"""
if not isinstance(server_object, FastMCP):
logger.error(
f"The server object {object_name} is of type "
f"{type(server_object)} (expecting {FastMCP})."
)
if isinstance(server_object, LowLevelServer):
logger.warning(
"Note that only FastMCP server is supported. Low level "
"Server class is not yet supported."
)
return False
return True

# If no object specified, try common server names
if not server_object:
# Look for the most common server object names
for name in ["mcp", "server", "app"]:
if hasattr(module, name):
if not _check_server_object(getattr(module, name), f"{file}:{name}"):
logger.error(
f"Ignoring object '{file}:{name}' as it's not a valid "
"server object"
)
continue
return getattr(module, name)

logger.error(
f"No server object found in {file}. Please either:\n"
"1. Use a standard variable name (mcp, server, or app)\n"
"2. Specify the object name with file:object syntax",
"2. Specify the object name with file:object syntax"
"3. If the server creates the FastMCP object within main() "
" or another function, refactor the FastMCP object to be a "
" global variable named mcp, server, or app.",
extra={"file": str(file)},
)
sys.exit(1)
Expand Down Expand Up @@ -179,6 +213,9 @@ def _import_server(file: Path, server_object: str | None = None):
)
sys.exit(1)

if not _check_server_object(server, server_object):
sys.exit(1)

return server


Expand Down