Skip to content

Commit d821825

Browse files
committed
apply suggestin
1 parent 80614da commit d821825

2 files changed

Lines changed: 22 additions & 20 deletions

File tree

src/litserve/mcp.py

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

23+
from fastapi import FastAPI
2324
from pydantic import BaseModel
2425
from starlette.applications import Starlette
2526
from starlette.routing import Mount
2627
from starlette.types import Receive, Scope, Send
2728

2829
from litserve.utils import is_package_installed
2930

30-
if not is_package_installed("mcp"):
31-
raise RuntimeError(
32-
"mcp package is required for MCP support. To install, run `pip install mcp[cli]` in the terminal."
33-
)
31+
_is_mcp_installed = is_package_installed("fastmcp")
3432

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
33+
34+
if _is_mcp_installed:
35+
from fastapi import FastAPI
36+
from mcp.server.fastmcp.server import _convert_to_content
37+
from mcp.server.lowlevel import Server as MCPServer
38+
from mcp.server.streamable_http_manager import StreamableHTTPSessionManager
39+
from mcp.types import Tool as ToolType
40+
41+
else:
42+
ToolType = object
4043

4144
if TYPE_CHECKING:
4245
from litserve.api import LitAPI
@@ -249,7 +252,7 @@ async def _call_handler(handler, **kwargs):
249252
return _convert_to_content(await handler(*bound.args, **bound.kwargs))
250253

251254

252-
class ToolEndpointType(types.Tool):
255+
class ToolEndpointType(ToolType):
253256
endpoint: str
254257

255258

@@ -354,6 +357,11 @@ def __init__(
354357
self.input_schema = input_schema
355358
self._connected = False
356359

360+
if not is_package_installed("fastmcp"):
361+
raise RuntimeError(
362+
"mcp package is required for MCP support. To install, run `pip install fastmcp` in the terminal."
363+
)
364+
357365
@property
358366
def name(self) -> str:
359367
return self._name
@@ -370,11 +378,6 @@ def _connect(self, lit_api: "LitAPI"):
370378
self._connected = True
371379

372380
def as_tool(self) -> ToolEndpointType:
373-
if not is_package_installed("mcp"):
374-
raise RuntimeError(
375-
"fastmcp package is required for MCP support. To install, run `pip install mcp[cli]` in the terminal."
376-
)
377-
378381
if not self._connected:
379382
raise RuntimeError("MCP is not connected to a LitAPI.")
380383

@@ -406,7 +409,7 @@ def as_tool(self) -> ToolEndpointType:
406409

407410

408411
class _MCPRequestHandler:
409-
def __init__(self, mcp_server: MCPServer):
412+
def __init__(self, mcp_server: "MCPServer"):
410413
self.mcp_server = mcp_server
411414
self._session_manager = None
412415

@@ -542,7 +545,7 @@ async def _call_tool(name: str, arguments: dict):
542545
starlette_app = self.request_handler.streamable_http_app()
543546
app.mount("/", starlette_app, name="mcp")
544547

545-
def connect_mcp_server(self, mcp_tools: List[types.Tool], app: FastAPI):
548+
def connect_mcp_server(self, mcp_tools: List[ToolType], app: FastAPI):
546549
"""LitServer calls this method to connect MCP server to the FastAPI app.
547550
548551
Args:

tests/unit/test_mcp.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@
2525
def test_mcp_not_installed(mock_is_package_installed):
2626
with pytest.raises(
2727
RuntimeError,
28-
match="fastmcp package is required for MCP support. To install, run `pip install fastmcp` in the terminal.",
28+
match="mcp package is required for MCP support. To install, run `pip install fastmcp` in the terminal.",
2929
):
30-
MCP().as_tool()
31-
mock_is_package_installed.assert_called_once_with("fastmcp")
30+
MCP()
3231

3332

3433
def test_python_type_to_json_schema():

0 commit comments

Comments
 (0)