11import httpx
22import yaml
33from fastmcp import FastMCP
4+ from fastmcp .server .openapi import FastMCPOpenAPI
45from fastmcp .utilities .openapi import OpenAPIParser
6+ from fastmcp .server .openapi import MCPType , RouteMap
7+ from fastmcp .resources .resource_manager import ResourceManager
58from .settings import settings , basic_endpoints
69from .tools import register_custom_tools
710from .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
5061async 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
6674async 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 } " )
0 commit comments