← Về thư mục
📄 / / integrations / notebooklm-mcp-cli / .venv / lib / python3.11 / site-packages / fastmcp / server / providers / openapi / README.md

OpenAPI Server Implementation (New)

This directory contains the next-generation FastMCP server implementation for OpenAPI integration, designed to replace the legacy implementation in /server/openapi.py.

Architecture Overview

The new implementation uses a stateless request building approach with openapi-core and RequestDirector, providing zero-latency startup and robust OpenAPI support optimized for serverless environments.

Core Components

  1. server.py - FastMCPOpenAPI main server class with RequestDirector integration
  2. components.py - Simplified component implementations using RequestDirector
  3. routing.py - Route mapping and component selection logic

Key Architecture Principles

1. Stateless Performance

2. Unified Implementation

3. OpenAPI Compliance

Component Classes

RequestDirector-Based Components

OpenAPITool

OpenAPIResource / OpenAPIResourceTemplate

Server Implementation

FastMCPOpenAPI Class

The main server class orchestrates the stateless request building approach:

class FastMCPOpenAPI(FastMCP):
    def __init__(self, openapi_spec: dict, client: httpx.AsyncClient, **kwargs):
        # 1. Parse OpenAPI spec to HTTP routes with pre-calculated schemas
        self._routes = parse_openapi_to_http_routes(openapi_spec)

        # 2. Initialize RequestDirector with openapi-core Spec
        self._spec = Spec.from_dict(openapi_spec)
        self._director = RequestDirector(self._spec)

        # 3. Create components using RequestDirector
        self._create_components()

Component Creation Logic

def _create_tool(self, route: HTTPRoute) -> Tool:
    # All tools use RequestDirector for consistent, high-performance request building
    return OpenAPITool(
        client=self._client, 
        route=route, 
        director=self._director,
        name=tool_name,
        description=description,
        parameters=flat_param_schema
    )

Data Flow

Stateless Request Building

OpenAPI Spec → HTTPRoute with Pre-calculated Fields → RequestDirector → HTTP Request → Structured Response
  1. Spec Parsing: OpenAPI spec parsed to HTTPRoute models with pre-calculated schemas
  2. RequestDirector Setup: openapi-core Spec initialized for request building
  3. Component Creation: Create components with RequestDirector reference
  4. Request Building: RequestDirector builds HTTP request from flat parameters
  5. Request Execution: Execute request with httpx client
  6. Response Processing: Return structured MCP response

Key Features

1. Enhanced Parameter Handling

Parameter Collision Resolution

DeepObject Style Support

2. Robust Error Handling

HTTP Error Mapping

Request Building Error Handling

3. Performance Optimizations

Efficient Client Reuse

Request Optimization

Configuration

Server Options

server = FastMCPOpenAPI(
    openapi_spec=spec,           # Required: OpenAPI specification
    client=httpx_client,         # Required: HTTP client instance
    name="API Server",           # Optional: Server name
    route_map=custom_routes,     # Optional: Custom route mappings
    enable_caching=True,         # Optional: Enable response caching
)

Route Mapping Customization

from fastmcp.server.openapi_new.routing import RouteMap

custom_routes = RouteMap({
    "GET:/users": "tool",        # Force specific operations to be tools
    "GET:/status": "resource",   # Force specific operations to be resources
})

Testing Strategy

Test Structure

Tests are organized by functionality: - test_server.py - Server integration and RequestDirector behavior - test_parameter_collisions.py - Parameter collision handling - test_deepobject_style.py - DeepObject parameter style support - test_openapi_features.py - General OpenAPI feature compliance

Testing Philosophy

  1. Real Integration: Test with real OpenAPI specs and HTTP clients
  2. Minimal Mocking: Only mock external API endpoints
  3. Behavioral Focus: Test behavior, not implementation details
  4. Performance Focus: Test that initialization is fast and stateless

Example Test Pattern

async def test_stateless_request_building():
    """Test that server works with stateless RequestDirector approach."""

    # Test server initialization is fast
    start_time = time.time()
    server = FastMCPOpenAPI(spec=valid_spec, client=client)
    init_time = time.time() - start_time
    assert init_time < 0.01  # Should be very fast

    # Verify RequestDirector functionality
    assert hasattr(server, '_director')
    assert hasattr(server, '_spec')

Migration Benefits

From Legacy Implementation

  1. Eliminated Startup Latency: Zero code generation overhead (100-200ms improvement)
  2. Better OpenAPI Compliance: openapi-core handles all OpenAPI features correctly
  3. Serverless Friendly: Perfect for cold-start environments
  4. Simplified Architecture: Single RequestDirector approach eliminates complexity
  5. Enhanced Reliability: No dynamic code generation failures

Backward Compatibility

Monitoring and Debugging

Logging

# Enable debug logging to see implementation choices
import logging
logging.getLogger("fastmcp.server.openapi_new").setLevel(logging.DEBUG)

Key Log Messages

Debugging Common Issues

  1. RequestDirector Initialization Fails
  2. Check OpenAPI spec validity with openapi-core
  3. Verify spec format is correct JSON/YAML
  4. Ensure all required OpenAPI fields are present

  5. Parameter Issues

  6. Enable debug logging for parameter processing
  7. Check for parameter collision warnings
  8. Verify OpenAPI spec parameter definitions

  9. Performance Issues

  10. Monitor RequestDirector request building timing
  11. Check HTTP client configuration
  12. Review response processing timing

Future Enhancements

Planned Features

  1. Advanced Caching: Intelligent response caching with TTL
  2. Streaming Support: Handle streaming API responses
  3. Batch Operations: Optimize multiple operation calls
  4. Enhanced Monitoring: Detailed metrics and health checks
  5. Configuration Management: Dynamic configuration updates

Performance Improvements

  1. Enhanced Schema Caching: More aggressive schema pre-calculation
  2. Parallel Processing: Concurrent operation execution
  3. Memory Optimization: Further reduce memory footprint
  4. Request Optimization: Smart request batching and deduplication