Skip to content

Commit f1063d4

Browse files
authored
Merge pull request #19 from h2oai/mn/upgrade-dependencies
Upgrade FastMCP library
2 parents 4738e94 + 1ff54ee commit f1063d4

File tree

4 files changed

+37
-16
lines changed

4 files changed

+37
-16
lines changed

requirements.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
fastmcp==2.6.1
2-
pyyaml==6.0.2
1+
fastmcp==2.13.0.2
2+
pyyaml==6.0.3
33
httpx==0.28.1

src/h2ogpte_mcp_server/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = "0.1.5-dev"
1+
__version__ = "0.1.5"
22

33
import asyncio
44
from .server import start_server

src/h2ogpte_mcp_server/server.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import httpx
22
import yaml
33
from fastmcp import FastMCP
4+
from fastmcp.server.openapi import FastMCPOpenAPI
45
from fastmcp.utilities.openapi import OpenAPIParser
6+
from fastmcp.server.openapi import MCPType, RouteMap
7+
from fastmcp.resources.resource_manager import ResourceManager
58
from .settings import settings, basic_endpoints
69
from .tools import register_custom_tools
710
from .settings import EndpointSet
@@ -18,14 +21,23 @@ async def start_server():
1821
headers = {"Authorization": f"Bearer {settings.api_key}"}
1922
client = httpx.AsyncClient(base_url=f"{mux_service_url}/api/v1", headers=headers)
2023

21-
OpenAPIParser._convert_to_parameter_location = _patched_convert_to_parameter_location
24+
# Default route maps for FastMCP 2.6.1
25+
route_maps = [
26+
RouteMap(methods=["GET"], pattern=r".*\{.*\}.*", mcp_type=MCPType.RESOURCE_TEMPLATE),
27+
RouteMap(methods=["GET"], pattern=r".*", mcp_type=MCPType.RESOURCE),
28+
RouteMap(methods="*", pattern=r".*", mcp_type=MCPType.TOOL),
29+
]
2230

31+
if settings.all_endpoints_as_tools:
32+
route_maps = [RouteMap(methods="*", pattern=r".*", mcp_type=MCPType.TOOL)]
33+
2334
# Create the MCP server
24-
mcp = FastMCP.from_openapi(
35+
# Enforcing the old parser, the experimental parser does not work as expected.
36+
mcp = FastMCPOpenAPI(
2537
openapi_spec=openapi_spec,
2638
client=client,
2739
name="H2OGPTe MCP API server",
28-
all_routes_as_tools=settings.all_endpoints_as_tools
40+
route_maps=route_maps,
2941
)
3042

3143
await register_custom_tools(mcp)
@@ -44,7 +56,6 @@ async def start_server():
4456
elif settings.endpoint_set == EndpointSet.ALL:
4557
pass
4658

47-
4859
await mcp.run_async()
4960

5061
async def load_openapi_spec(mux_service_url):
@@ -59,9 +70,6 @@ async def load_openapi_spec(mux_service_url):
5970
openapi_spec = yaml.load(yaml_spec, Loader=yaml.CLoader)
6071
return openapi_spec
6172

62-
def _patched_convert_to_parameter_location(self, param_in: "ParameterLocation") -> str:
63-
return param_in.value
64-
6573

6674
async def remove_create_job_tools(mcp: FastMCP):
6775
tools = await mcp.get_tools()
@@ -79,7 +87,17 @@ async def reduce_tools_and_resources(mcp: FastMCP, endpoints: List[str]):
7987
mcp.remove_tool(tool)
8088

8189
resources = await mcp.get_resources()
82-
for resource in resources.keys():
83-
if resource not in endpoints:
84-
print(f"Skipping resource {resource}")
85-
mcp.remove_resource(resource)
90+
resource_templates = await mcp.get_resource_templates()
91+
mcp._resource_manager = ResourceManager()
92+
93+
for resource_name, resource in resources.items():
94+
if resource_name in endpoints:
95+
mcp.add_resource(resource)
96+
else:
97+
print(f"Skipping resource {resource_name}")
98+
99+
for resource_template_name, resource_template in resource_templates.items():
100+
if resource_template_name in endpoints:
101+
mcp.add_resource_template(resource_template)
102+
else:
103+
print(f"Skipping resource template {resource_template_name}")

src/h2ogpte_mcp_server/tools.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from pathlib import Path
22
from fastmcp import FastMCP, Context
3+
from fastmcp.tools import Tool
34

45

56
async def upload_file(file: str, context: Context) -> str:
@@ -51,10 +52,12 @@ async def register_custom_tools(mcp: FastMCP):
5152
print("Overriding upload_file tool")
5253
mcp.remove_tool("upload_file")
5354
tool = tools["upload_file"]
54-
mcp.add_tool(upload_file, name="upload_file", description=tool.description)
55+
tool = Tool.from_function(name="upload_file", description=tool.description, fn=upload_file)
56+
mcp.add_tool(tool)
5557

5658
if "update_collection_thumbnail" in tools:
5759
print("Overriding update_collection_thumbnail tool")
5860
mcp.remove_tool("update_collection_thumbnail")
5961
tool = tools["update_collection_thumbnail"]
60-
mcp.add_tool(update_collection_thumbnail, name="update_collection_thumbnail", description=tool.description)
62+
tool = Tool.from_function(name="update_collection_thumbnail", description=tool.description, fn=update_collection_thumbnail)
63+
mcp.add_tool(tool)

0 commit comments

Comments
 (0)