Skip to content

Commit a97c227

Browse files
committed
MCP package dependency check.
- Added a runtime check for the "fastmcp" package in `mcp.py`, raising an error if it is not installed. - Updated the error message to provide clearer installation instructions.
1 parent 9da3290 commit a97c227

2 files changed

Lines changed: 24 additions & 7 deletions

File tree

src/litserve/mcp.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,24 @@
2020
from contextlib import asynccontextmanager
2121
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union, get_args, get_origin
2222

23-
import mcp.types as types
24-
from fastapi import FastAPI
25-
from mcp.server.fastmcp.server import _convert_to_content
26-
from mcp.server.lowlevel import Server as MCPServer
27-
from mcp.server.streamable_http_manager import StreamableHTTPSessionManager
2823
from pydantic import BaseModel
2924
from starlette.applications import Starlette
3025
from starlette.routing import Mount
3126
from starlette.types import Receive, Scope, Send
3227

3328
from litserve.utils import is_package_installed
3429

30+
if not is_package_installed("fastmcp"):
31+
raise RuntimeError(
32+
"mcp package is required for MCP support. To install, run `pip install fastmcp` in the terminal."
33+
)
34+
35+
import mcp.types as types
36+
from fastapi import FastAPI
37+
from mcp.server.fastmcp.server import _convert_to_content
38+
from mcp.server.lowlevel import Server as MCPServer
39+
from mcp.server.streamable_http_manager import StreamableHTTPSessionManager
40+
3541
if TYPE_CHECKING:
3642
from litserve.api import LitAPI
3743

@@ -364,9 +370,9 @@ def _connect(self, lit_api: "LitAPI"):
364370
self._connected = True
365371

366372
def as_tool(self) -> ToolEndpointType:
367-
if not is_package_installed("mcp"):
373+
if not is_package_installed("fastmcp"):
368374
raise RuntimeError(
369-
"MCP is not installed. Please install it with `pip install mcp[cli]` or `uv pip install mcp[cli]`"
375+
"fastmcp package is required for MCP support. To install, run `pip install fastmcp` in the terminal."
370376
)
371377

372378
if not self._connected:

tests/unit/test_mcp.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import inspect
22
import sys
33
from typing import Dict, List, Optional
4+
from unittest.mock import patch
45

56
import pytest
67
from fastapi import FastAPI
@@ -20,6 +21,16 @@
2021
)
2122

2223

24+
@patch("litserve.mcp.is_package_installed", return_value=False)
25+
def test_mcp_not_installed(mock_is_package_installed):
26+
with pytest.raises(
27+
RuntimeError,
28+
match="fastmcp package is required for MCP support. To install, run `pip install fastmcp` in the terminal.",
29+
):
30+
MCP().as_tool()
31+
mock_is_package_installed.assert_called_once_with("fastmcp")
32+
33+
2334
def test_python_type_to_json_schema():
2435
assert _python_type_to_json_schema(inspect.Parameter.empty) == "string"
2536
assert _python_type_to_json_schema(int) == "integer"

0 commit comments

Comments
 (0)