Spaces:
Running
Running
File size: 24,170 Bytes
96618d0 3d30e82 96618d0 007c3be 96618d0 9cc5eae 3d30e82 96618d0 c2a9892 96618d0 2c75133 96618d0 9dc2a8a 96618d0 dc0a2a3 96618d0 3d30e82 96618d0 dc0a2a3 96618d0 9822b73 4805965 c2a9892 007c3be 96618d0 9822b73 c2a9892 007c3be 96618d0 4805965 96618d0 e1a4799 96618d0 70737e6 96618d0 70737e6 96618d0 70737e6 2c75133 70737e6 96618d0 70737e6 96618d0 70737e6 96618d0 4805965 96618d0 9dc2a8a 1534a61 9dc2a8a 96618d0 1534a61 9dc2a8a 96618d0 9822b73 4805965 96618d0 9822b73 96618d0 4805965 96618d0 e1a4799 1534a61 96618d0 6c500ce 96618d0 11780c0 96618d0 97708fa 11780c0 520d656 11780c0 520d656 5f519d8 0cdb454 11780c0 96618d0 70737e6 96618d0 70737e6 4805965 96618d0 d2069f2 e1fcc29 d2069f2 96618d0 d2069f2 96618d0 9822b73 4805965 96618d0 e1fcc29 96618d0 9822b73 96618d0 4805965 96618d0 e1a4799 3d30e82 1534a61 3d30e82 96618d0 e1fcc29 9822b73 4805965 96618d0 4805965 96618d0 defdbf4 96618d0 4805965 96618d0 71d1f33 96618d0 71d1f33 96618d0 9822b73 4805965 96618d0 9822b73 96618d0 71d1f33 96618d0 71d1f33 96618d0 9822b73 4805965 96618d0 9822b73 96618d0 71d1f33 96618d0 71d1f33 96618d0 71d1f33 96618d0 9822b73 4805965 96618d0 9822b73 96618d0 81e8514 1534a61 96618d0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 | """FastMCP server implementation for OpenAPI integration."""
from __future__ import annotations
import enum
import json
import re
from collections.abc import Callable
from dataclasses import dataclass
from re import Pattern
from typing import TYPE_CHECKING, Any, Literal
import httpx
from mcp.types import EmbeddedResource, ImageContent, TextContent, ToolAnnotations
from pydantic.networks import AnyUrl
from fastmcp.exceptions import ToolError
from fastmcp.resources import Resource, ResourceTemplate
from fastmcp.server.server import FastMCP
from fastmcp.tools.tool import Tool, _convert_to_content
from fastmcp.utilities import openapi
from fastmcp.utilities.logging import get_logger
from fastmcp.utilities.openapi import (
_combine_schemas,
format_description_with_responses,
)
if TYPE_CHECKING:
from fastmcp.server import Context
logger = get_logger(__name__)
HttpMethod = Literal["GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD"]
class RouteType(enum.Enum):
"""Type of FastMCP component to create from a route."""
TOOL = "TOOL"
RESOURCE = "RESOURCE"
RESOURCE_TEMPLATE = "RESOURCE_TEMPLATE"
PROMPT = "PROMPT"
IGNORE = "IGNORE"
@dataclass
class RouteMap:
"""Mapping configuration for HTTP routes to FastMCP component types."""
methods: list[HttpMethod]
pattern: Pattern[str] | str
route_type: RouteType
# Default route mappings as a list, where order determines priority
DEFAULT_ROUTE_MAPPINGS = [
# GET requests with path parameters go to ResourceTemplate
RouteMap(
methods=["GET"], pattern=r".*\{.*\}.*", route_type=RouteType.RESOURCE_TEMPLATE
),
# GET requests without path parameters go to Resource
RouteMap(methods=["GET"], pattern=r".*", route_type=RouteType.RESOURCE),
# All other HTTP methods go to Tool
RouteMap(
methods=["POST", "PUT", "PATCH", "DELETE", "OPTIONS", "HEAD"],
pattern=r".*",
route_type=RouteType.TOOL,
),
]
def _determine_route_type(
route: openapi.HTTPRoute,
mappings: list[RouteMap],
) -> RouteType:
"""
Determines the FastMCP component type based on the route and mappings.
Args:
route: HTTPRoute object
mappings: List of RouteMap objects in priority order
Returns:
RouteType for this route
"""
# Check mappings in priority order (first match wins)
for route_map in mappings:
# Check if the HTTP method matches
if route.method in route_map.methods:
# Handle both string patterns and compiled Pattern objects
if isinstance(route_map.pattern, Pattern):
pattern_matches = route_map.pattern.search(route.path)
else:
pattern_matches = re.search(route_map.pattern, route.path)
if pattern_matches:
logger.debug(
f"Route {route.method} {route.path} matched mapping to {route_map.route_type.name}"
)
return route_map.route_type
# Default fallback
return RouteType.TOOL
# Placeholder function to provide function metadata
async def _openapi_passthrough(*args, **kwargs):
"""Placeholder function for OpenAPI endpoints."""
# This is kept for metadata generation purposes
pass
class OpenAPITool(Tool):
"""Tool implementation for OpenAPI endpoints."""
def __init__(
self,
client: httpx.AsyncClient,
route: openapi.HTTPRoute,
name: str,
description: str,
parameters: dict[str, Any],
tags: set[str] = set(),
timeout: float | None = None,
annotations: ToolAnnotations | None = None,
serializer: Callable[[Any], str] | None = None,
):
super().__init__(
name=name,
description=description,
parameters=parameters,
fn=self._execute_request, # We'll use an instance method instead of a global function
tags=tags,
annotations=annotations,
serializer=serializer,
)
self._client = client
self._route = route
self._timeout = timeout
def __repr__(self) -> str:
"""Custom representation to prevent recursion errors when printing."""
return f"OpenAPITool(name={self.name!r}, method={self._route.method}, path={self._route.path})"
async def _execute_request(self, *args, **kwargs):
"""Execute the HTTP request based on the route configuration."""
context = kwargs.get("context")
# Prepare URL
path = self._route.path
# Replace path parameters with values from kwargs
# Path parameters should never be None as they're typically required
# but we'll handle that case anyway
path_params = {
p.name: kwargs.get(p.name)
for p in self._route.parameters
if p.location == "path"
and p.name in kwargs
and kwargs.get(p.name) is not None
}
# Ensure all path parameters are provided
required_path_params = {
p.name
for p in self._route.parameters
if p.location == "path" and p.required
}
missing_params = required_path_params - path_params.keys()
if missing_params:
raise ToolError(f"Missing required path parameters: {missing_params}")
for param_name, param_value in path_params.items():
path = path.replace(f"{{{param_name}}}", str(param_value))
# Prepare query parameters - filter out None and empty strings
query_params = {
p.name: kwargs.get(p.name)
for p in self._route.parameters
if p.location == "query"
and p.name in kwargs
and kwargs.get(p.name) is not None
and kwargs.get(p.name) != ""
}
# Prepare headers - fix typing by ensuring all values are strings
headers = {}
for p in self._route.parameters:
if (
p.location == "header"
and p.name in kwargs
and kwargs[p.name] is not None
):
headers[p.name] = str(kwargs[p.name])
# Prepare request body
json_data = None
if self._route.request_body and self._route.request_body.content_schema:
# Extract body parameters, excluding path/query/header params that were already used
path_query_header_params = {
p.name
for p in self._route.parameters
if p.location in ("path", "query", "header")
}
body_params = {
k: v
for k, v in kwargs.items()
if k not in path_query_header_params and k != "context"
}
if body_params:
json_data = body_params
# Log the request details if a context is available
if context:
try:
await context.info(f"Making {self._route.method} request to {path}")
except (ValueError, AttributeError):
# Silently continue if context logging is not available
pass
# Execute the request
try:
response = await self._client.request(
method=self._route.method,
url=path,
params=query_params,
headers=headers,
json=json_data,
timeout=self._timeout,
)
# Raise for 4xx/5xx responses
response.raise_for_status()
# Try to parse as JSON first
try:
return response.json()
except (json.JSONDecodeError, ValueError):
# Return text content if not JSON
return response.text
except httpx.HTTPStatusError as e:
# Handle HTTP errors (4xx, 5xx)
error_message = (
f"HTTP error {e.response.status_code}: {e.response.reason_phrase}"
)
try:
error_data = e.response.json()
error_message += f" - {error_data}"
except (json.JSONDecodeError, ValueError):
if e.response.text:
error_message += f" - {e.response.text}"
raise ValueError(error_message)
except httpx.RequestError as e:
# Handle request errors (connection, timeout, etc.)
raise ValueError(f"Request error: {str(e)}")
async def run(
self, arguments: dict[str, Any]
) -> list[TextContent | ImageContent | EmbeddedResource]:
"""Run the tool with arguments and optional context."""
response = await self._execute_request(**arguments)
return _convert_to_content(response)
class OpenAPIResource(Resource):
"""Resource implementation for OpenAPI endpoints."""
def __init__(
self,
client: httpx.AsyncClient,
route: openapi.HTTPRoute,
uri: str,
name: str,
description: str,
mime_type: str = "application/json",
tags: set[str] = set(),
timeout: float | None = None,
):
super().__init__(
uri=AnyUrl(uri), # Convert string to AnyUrl
name=name,
description=description,
mime_type=mime_type,
tags=tags,
)
self._client = client
self._route = route
self._timeout = timeout
def __repr__(self) -> str:
"""Custom representation to prevent recursion errors when printing."""
return f"OpenAPIResource(name={self.name!r}, uri={self.uri!r}, path={self._route.path})"
async def read(self) -> str | bytes:
"""Fetch the resource data by making an HTTP request."""
try:
# Extract path parameters from the URI if present
path = self._route.path
resource_uri = str(self.uri)
# If this is a templated resource, extract path parameters from the URI
if "{" in path and "}" in path:
# Extract the resource ID from the URI (the last part after the last slash)
parts = resource_uri.split("/")
if len(parts) > 1:
# Find all path parameters in the route path
path_params = {}
# Find the path parameter names from the route path
param_matches = re.findall(r"\{([^}]+)\}", path)
if param_matches:
# Reverse sorting from creation order (traversal is backwards)
param_matches.sort(reverse=True)
# Number of sent parameters is number of parts -1 (assuming first part is resource identifier)
expected_param_count = len(parts) - 1
# Map parameters from the end of the URI to the parameters in the path
# Last parameter in URI (parts[-1]) maps to last parameter in path, and so on
for i, param_name in enumerate(param_matches):
# Ensure we don't use resource identifier as parameter
if i < expected_param_count:
# Get values from the end of parts
param_value = parts[-1 - i]
path_params[param_name] = param_value
# Replace path parameters with their values
for param_name, param_value in path_params.items():
path = path.replace(f"{{{param_name}}}", str(param_value))
# Filter any query parameters - get query parameters and filter out None/empty values
query_params = {}
for param in self._route.parameters:
if param.location == "query" and hasattr(self, f"_{param.name}"):
value = getattr(self, f"_{param.name}")
if value is not None and value != "":
query_params[param.name] = value
response = await self._client.request(
method=self._route.method,
url=path,
params=query_params,
timeout=self._timeout,
)
# Raise for 4xx/5xx responses
response.raise_for_status()
# Determine content type and return appropriate format
content_type = response.headers.get("content-type", "").lower()
if "application/json" in content_type:
result = response.json()
return json.dumps(result)
elif any(ct in content_type for ct in ["text/", "application/xml"]):
return response.text
else:
return response.content
except httpx.HTTPStatusError as e:
# Handle HTTP errors (4xx, 5xx)
error_message = (
f"HTTP error {e.response.status_code}: {e.response.reason_phrase}"
)
try:
error_data = e.response.json()
error_message += f" - {error_data}"
except (json.JSONDecodeError, ValueError):
if e.response.text:
error_message += f" - {e.response.text}"
raise ValueError(error_message)
except httpx.RequestError as e:
# Handle request errors (connection, timeout, etc.)
raise ValueError(f"Request error: {str(e)}")
class OpenAPIResourceTemplate(ResourceTemplate):
"""Resource template implementation for OpenAPI endpoints."""
def __init__(
self,
client: httpx.AsyncClient,
route: openapi.HTTPRoute,
uri_template: str,
name: str,
description: str,
parameters: dict[str, Any],
tags: set[str] = set(),
timeout: float | None = None,
):
super().__init__(
uri_template=uri_template,
name=name,
description=description,
fn=lambda **kwargs: None,
parameters=parameters,
tags=tags,
)
self._client = client
self._route = route
self._timeout = timeout
def __repr__(self) -> str:
"""Custom representation to prevent recursion errors when printing."""
return f"OpenAPIResourceTemplate(name={self.name!r}, uri_template={self.uri_template!r}, path={self._route.path})"
async def create_resource(
self,
uri: str,
params: dict[str, Any],
context: Context | None = None,
) -> Resource:
"""Create a resource with the given parameters."""
# Generate a URI for this resource instance
uri_parts = []
for key, value in params.items():
uri_parts.append(f"{key}={value}")
# Create and return a resource
return OpenAPIResource(
client=self._client,
route=self._route,
uri=uri,
name=f"{self.name}-{'-'.join(uri_parts)}",
description=self.description or f"Resource for {self._route.path}",
mime_type="application/json",
tags=set(self._route.tags or []),
timeout=self._timeout,
)
class FastMCPOpenAPI(FastMCP):
"""
FastMCP server implementation that creates components from an OpenAPI schema.
This class parses an OpenAPI specification and creates appropriate FastMCP components
(Tools, Resources, ResourceTemplates) based on route mappings.
Example:
```python
from fastmcp.server.openapi import FastMCPOpenAPI, RouteMap, RouteType
import httpx
# Define custom route mappings
custom_mappings = [
# Map all user-related endpoints to ResourceTemplate
RouteMap(
methods=["GET", "POST", "PATCH"],
pattern=r".*/users/.*",
route_type=RouteType.RESOURCE_TEMPLATE
),
# Map all analytics endpoints to Tool
RouteMap(
methods=["GET"],
pattern=r".*/analytics/.*",
route_type=RouteType.TOOL
),
]
# Create server with custom mappings
server = FastMCPOpenAPI(
openapi_spec=spec,
client=httpx.AsyncClient(),
name="API Server",
route_maps=custom_mappings,
)
```
"""
def __init__(
self,
openapi_spec: dict[str, Any],
client: httpx.AsyncClient,
name: str | None = None,
route_maps: list[RouteMap] | None = None,
timeout: float | None = None,
**settings: Any,
):
"""
Initialize a FastMCP server from an OpenAPI schema.
Args:
openapi_spec: OpenAPI schema as a dictionary or file path
client: httpx AsyncClient for making HTTP requests
name: Optional name for the server
route_maps: Optional list of RouteMap objects defining route mappings
timeout: Optional timeout (in seconds) for all requests
**settings: Additional settings for FastMCP
"""
super().__init__(name=name or "OpenAPI FastMCP", **settings)
self._client = client
self._timeout = timeout
http_routes = openapi.parse_openapi_to_http_routes(openapi_spec)
# Process routes
route_maps = (route_maps or []) + DEFAULT_ROUTE_MAPPINGS
for route in http_routes:
# Determine route type based on mappings or default rules
route_type = _determine_route_type(route, route_maps)
# Use operation_id if available, otherwise generate a name
operation_id = route.operation_id
if not operation_id:
# Generate operation ID from method and path
path_parts = route.path.strip("/").split("/")
path_name = "_".join(p for p in path_parts if not p.startswith("{"))
operation_id = f"{route.method.lower()}_{path_name}"
if route_type == RouteType.TOOL:
self._create_openapi_tool(route, operation_id)
elif route_type == RouteType.RESOURCE:
self._create_openapi_resource(route, operation_id)
elif route_type == RouteType.RESOURCE_TEMPLATE:
self._create_openapi_template(route, operation_id)
elif route_type == RouteType.PROMPT:
# Not implemented yet
logger.warning(
f"PROMPT route type not implemented: {route.method} {route.path}"
)
elif route_type == RouteType.IGNORE:
logger.info(f"Ignoring route: {route.method} {route.path}")
logger.info(f"Created FastMCP OpenAPI server with {len(http_routes)} routes")
def _create_openapi_tool(self, route: openapi.HTTPRoute, operation_id: str):
"""Creates and registers an OpenAPITool with enhanced description."""
combined_schema = _combine_schemas(route)
tool_name = operation_id
base_description = (
route.description
or route.summary
or f"Executes {route.method} {route.path}"
)
# Format enhanced description with parameters and request body
enhanced_description = format_description_with_responses(
base_description=base_description,
responses=route.responses,
parameters=route.parameters,
request_body=route.request_body,
)
tool = OpenAPITool(
client=self._client,
route=route,
name=tool_name,
description=enhanced_description,
parameters=combined_schema,
tags=set(route.tags or []),
timeout=self._timeout,
)
# Register the tool by directly assigning to the tools dictionary
self._tool_manager._tools[tool_name] = tool
logger.debug(
f"Registered TOOL: {tool_name} ({route.method} {route.path}) with tags: {route.tags}"
)
def _create_openapi_resource(self, route: openapi.HTTPRoute, operation_id: str):
"""Creates and registers an OpenAPIResource with enhanced description."""
resource_name = operation_id
resource_uri = f"resource://openapi/{resource_name}"
base_description = (
route.description or route.summary or f"Represents {route.path}"
)
# Format enhanced description with parameters and request body
enhanced_description = format_description_with_responses(
base_description=base_description,
responses=route.responses,
parameters=route.parameters,
request_body=route.request_body,
)
resource = OpenAPIResource(
client=self._client,
route=route,
uri=resource_uri,
name=resource_name,
description=enhanced_description,
tags=set(route.tags or []),
timeout=self._timeout,
)
# Register the resource by directly assigning to the resources dictionary
self._resource_manager._resources[str(resource.uri)] = resource
logger.debug(
f"Registered RESOURCE: {resource_uri} ({route.method} {route.path}) with tags: {route.tags}"
)
def _create_openapi_template(self, route: openapi.HTTPRoute, operation_id: str):
"""Creates and registers an OpenAPIResourceTemplate with enhanced description."""
template_name = operation_id
path_params = [p.name for p in route.parameters if p.location == "path"]
path_params.sort() # Sort for consistent URIs
uri_template_str = f"resource://openapi/{template_name}"
if path_params:
uri_template_str += "/" + "/".join(f"{{{p}}}" for p in path_params)
base_description = (
route.description or route.summary or f"Template for {route.path}"
)
# Format enhanced description with parameters and request body
enhanced_description = format_description_with_responses(
base_description=base_description,
responses=route.responses,
parameters=route.parameters,
request_body=route.request_body,
)
template_params_schema = {
"type": "object",
"properties": {
p.name: {
**(p.schema_.copy() if isinstance(p.schema_, dict) else {}),
**(
{"description": p.description}
if p.description
and not (
isinstance(p.schema_, dict) and "description" in p.schema_
)
else {}
),
}
for p in route.parameters
if p.location == "path"
},
"required": [
p.name for p in route.parameters if p.location == "path" and p.required
],
}
template = OpenAPIResourceTemplate(
client=self._client,
route=route,
uri_template=uri_template_str,
name=template_name,
description=enhanced_description,
parameters=template_params_schema,
tags=set(route.tags or []),
timeout=self._timeout,
)
# Register the template by directly assigning to the templates dictionary
self._resource_manager._templates[uri_template_str] = template
logger.debug(
f"Registered TEMPLATE: {uri_template_str} ({route.method} {route.path}) with tags: {route.tags}"
)
async def _mcp_call_tool(self, name: str, arguments: dict[str, Any]) -> Any:
"""Override the call_tool method to return the raw result without converting to content."""
result = await self._tool_manager.call_tool(name, arguments)
return result
|