Skip to content

Commit 8b78afb

Browse files
committed
fix: Add checks and better error messages on server object for mcp run/dev
mcp run/dev command expects the server object in the python file to be a global variable, and it must be of the FastMCP type and not the low level Server type. The previous error messages may be confusing to users who are not aware of these facts.
1 parent c897868 commit 8b78afb

File tree

1 file changed

+38
-2
lines changed

1 file changed

+38
-2
lines changed

src/mcp/cli/cli.py

+38-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import os
66
import subprocess
77
import sys
8+
from mcp.server import FastMCP
9+
from mcp.server import Server as LowLevelServer
810
from pathlib import Path
9-
from typing import Annotated
11+
from typing import Annotated, Any
1012

1113
try:
1214
import typer
@@ -141,17 +143,48 @@ def _import_server(file: Path, server_object: str | None = None):
141143
module = importlib.util.module_from_spec(spec)
142144
spec.loader.exec_module(module)
143145

146+
def _check_server_object(server_object: Any, object_name: str):
147+
"""Helper function to check that the server object is supported
148+
149+
Args:
150+
server_object: The server object to check.
151+
152+
Returns:
153+
True if it's supported.
154+
"""
155+
if not isinstance(server_object, FastMCP):
156+
logger.error(
157+
f"The server object {object_name} is of type "
158+
f"{type(server_object)} (expecting <class 'FastMCP'>)."
159+
)
160+
if isinstance(server_object, LowLevelServer):
161+
logger.warn(
162+
"Note that only FastMCP server is supported. Low level "
163+
"Server class is not yet supported."
164+
)
165+
return False
166+
return True
167+
144168
# If no object specified, try common server names
145169
if not server_object:
146170
# Look for the most common server object names
147171
for name in ["mcp", "server", "app"]:
148172
if hasattr(module, name):
173+
if not _check_server_object(getattr(module, name), f"{file}:{name}"):
174+
logger.error(
175+
f"Ignoring object '{file}:{name}' as it's not a valid "
176+
"server object"
177+
)
178+
continue
149179
return getattr(module, name)
150180

151181
logger.error(
152182
f"No server object found in {file}. Please either:\n"
153183
"1. Use a standard variable name (mcp, server, or app)\n"
154-
"2. Specify the object name with file:object syntax",
184+
"2. Specify the object name with file:object syntax"
185+
"3. If the server creates the FastMCP object within main() "
186+
" or another function, refactor the FastMCP object to be a "
187+
" global variable named mcp, server, or app.",
155188
extra={"file": str(file)},
156189
)
157190
sys.exit(1)
@@ -172,6 +205,9 @@ def _import_server(file: Path, server_object: str | None = None):
172205
# Just object name
173206
server = getattr(module, server_object, None)
174207

208+
if not _check_server_object(server, server_object):
209+
server = None
210+
175211
if server is None:
176212
logger.error(
177213
f"Server object '{server_object}' not found",

0 commit comments

Comments
 (0)