MCPcopy
hub / github.com/modelcontextprotocol/python-sdk / run

Function run

src/mcp/cli/cli.py:304–357  ·  view source on GitHub ↗

Run an MCP server. The server can be specified in two ways: 1. Module approach: server.py - runs the module directly, expecting a server.run() call. 2. Import approach: server.py:app - imports and runs the specified server object. Note: This command runs the server directly. You ar

(
    file_spec: str = typer.Argument(
        ...,
        help="Python file to run, optionally with :object suffix",
    ),
    transport: Annotated[
        str | None,
        typer.Option(
            "--transport",
            "-t",
            help="Transport protocol to use (stdio or sse)",
        ),
    ] = None,
)

Source from the content-addressed store, hash-verified

302
303@app.command()
304def run(
305 file_spec: str = typer.Argument(
306 ...,
307 help="Python file to run, optionally with :object suffix",
308 ),
309 transport: Annotated[
310 str | None,
311 typer.Option(
312 "--transport",
313 "-t",
314 help="Transport protocol to use (stdio or sse)",
315 ),
316 ] = None,
317) -> None: # pragma: no cover
318 """Run an MCP server.
319
320 The server can be specified in two ways:
321 1. Module approach: server.py - runs the module directly, expecting a server.run() call.
322 2. Import approach: server.py:app - imports and runs the specified server object.
323
324 Note: This command runs the server directly. You are responsible for ensuring
325 all dependencies are available.
326 For dependency management, use `mcp install` or `mcp dev` instead.
327 """ # noqa: E501
328 file, server_object = _parse_file_path(file_spec)
329
330 logger.debug(
331 "Running server",
332 extra={
333 "file": str(file),
334 "server_object": server_object,
335 "transport": transport,
336 },
337 )
338
339 try:
340 # Import and get server object
341 server = _import_server(file, server_object)
342
343 # Run the server
344 kwargs = {}
345 if transport:
346 kwargs["transport"] = transport
347
348 server.run(**kwargs)
349
350 except Exception:
351 logger.exception(
352 "Failed to run server",
353 extra={
354 "file": str(file),
355 },
356 )
357 sys.exit(1)
358
359
360@app.command()

Callers

nothing calls this directly

Calls 5

_parse_file_pathFunction · 0.85
_import_serverFunction · 0.85
debugMethod · 0.80
runMethod · 0.45
exitMethod · 0.45

Tested by

no test coverage detected