mcp.py•2.1 kB
"""
Model Context Protocol (MCP) configuration.
This module configures the FastAPI MCP integration, which allows exposing
FastAPI endpoints as Model Context Protocol (MCP) tools for AI agents.
"""
from fastapi import FastAPI
from fastapi_mcp import FastApiMCP
import logging
from src.config import settings
# Configure logger for MCP requests
logger = logging.getLogger("mcp_logger")
def setup_mcp(app: FastAPI) -> FastApiMCP:
"""
Set up and configure the MCP server for the FastAPI application.
This function configures the FastAPI-MCP integration, which allows exposing
FastAPI endpoints as Model Context Protocol (MCP) tools for AI agents.
Args:
app: The FastAPI application instance.
Returns:
The configured FastApiMCP instance.
"""
# Get API key from settings
# api_key = settings.UNSPLASH_CLIENT_ID
# Create authentication config if API key is available
# auth_config: Optional[AuthConfig] = None
# if api_key:
# In a real application, you would implement proper authentication
# This is just a placeholder for demonstration purposes
# Note: AuthConfig requires at least one of these fields:
# - dependencies: List of FastAPI dependencies for authentication
# - issuer: OAuth issuer URL
# - custom_oauth_metadata: Custom OAuth metadata
# auth_config = AuthConfig(
# # Empty list means no authentication is required, but satisfies the validation
# dependencies=[]
# )
# Create MCP server with enhanced configuration
mcp = FastApiMCP(
app,
name=f"{settings.PROJECT_NAME} MCP",
description="MCP server for Unsplash API, providing tools for AI agents to interact with image data.",
# Enable authentication if configured
# auth_config=auth_config,
describe_all_responses=True,
describe_full_response_schema=True,
# Only include endpoints with these tags
include_tags=["photos", "search", "random"],
)
# Mount the MCP server directly to the FastAPI app
mcp.mount()
return mcp