forked from cardosofelipe/fast-next-template
feat(backend): implement MCP client infrastructure (#55)
Core MCP client implementation with comprehensive tooling:
**Services:**
- MCPClientManager: Main facade for all MCP operations
- MCPServerRegistry: Thread-safe singleton for server configs
- ConnectionPool: Connection pooling with auto-reconnection
- ToolRouter: Automatic tool routing with circuit breaker
- AsyncCircuitBreaker: Custom async-compatible circuit breaker
**Configuration:**
- YAML-based config with Pydantic models
- Environment variable expansion support
- Transport types: HTTP, SSE, STDIO
**API Endpoints:**
- GET /mcp/servers - List all MCP servers
- GET /mcp/servers/{name}/tools - List server tools
- GET /mcp/tools - List all tools from all servers
- GET /mcp/health - Health check all servers
- POST /mcp/call - Execute tool (admin only)
- GET /mcp/circuit-breakers - Circuit breaker status
- POST /mcp/circuit-breakers/{name}/reset - Reset circuit breaker
- POST /mcp/servers/{name}/reconnect - Force reconnection
**Testing:**
- 156 unit tests with comprehensive coverage
- Tests for all services, routes, and error handling
- Proper mocking and async test support
**Documentation:**
- MCP_CLIENT.md with usage examples
- Phase 2+ workflow documentation
🤖 Generated with [Claude Code](https://claude.com/claude-code)
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
85
backend/app/services/mcp/__init__.py
Normal file
85
backend/app/services/mcp/__init__.py
Normal file
@@ -0,0 +1,85 @@
|
||||
"""
|
||||
MCP Client Service Package
|
||||
|
||||
Provides infrastructure for communicating with MCP (Model Context Protocol)
|
||||
servers. This is the foundation for AI agent tool integration.
|
||||
|
||||
Usage:
|
||||
from app.services.mcp import get_mcp_client, MCPClientManager
|
||||
|
||||
# In FastAPI route
|
||||
async def my_route(mcp: MCPClientManager = Depends(get_mcp_client)):
|
||||
result = await mcp.call_tool("llm-gateway", "chat", {"prompt": "Hello"})
|
||||
|
||||
# Direct usage
|
||||
manager = MCPClientManager()
|
||||
await manager.initialize()
|
||||
result = await manager.call_tool("issues", "create_issue", {...})
|
||||
await manager.shutdown()
|
||||
"""
|
||||
|
||||
from .client_manager import (
|
||||
MCPClientManager,
|
||||
ServerHealth,
|
||||
get_mcp_client,
|
||||
reset_mcp_client,
|
||||
shutdown_mcp_client,
|
||||
)
|
||||
from .config import (
|
||||
MCPConfig,
|
||||
MCPServerConfig,
|
||||
TransportType,
|
||||
create_default_config,
|
||||
load_mcp_config,
|
||||
)
|
||||
from .connection import ConnectionPool, ConnectionState, MCPConnection
|
||||
from .exceptions import (
|
||||
MCPCircuitOpenError,
|
||||
MCPConnectionError,
|
||||
MCPError,
|
||||
MCPServerNotFoundError,
|
||||
MCPTimeoutError,
|
||||
MCPToolError,
|
||||
MCPToolNotFoundError,
|
||||
MCPValidationError,
|
||||
)
|
||||
from .registry import MCPServerRegistry, ServerCapabilities, get_registry
|
||||
from .routing import AsyncCircuitBreaker, CircuitState, ToolInfo, ToolResult, ToolRouter
|
||||
|
||||
__all__ = [
|
||||
# Main facade
|
||||
"MCPClientManager",
|
||||
"get_mcp_client",
|
||||
"shutdown_mcp_client",
|
||||
"reset_mcp_client",
|
||||
"ServerHealth",
|
||||
# Configuration
|
||||
"MCPConfig",
|
||||
"MCPServerConfig",
|
||||
"TransportType",
|
||||
"load_mcp_config",
|
||||
"create_default_config",
|
||||
# Registry
|
||||
"MCPServerRegistry",
|
||||
"ServerCapabilities",
|
||||
"get_registry",
|
||||
# Connection
|
||||
"ConnectionPool",
|
||||
"ConnectionState",
|
||||
"MCPConnection",
|
||||
# Routing
|
||||
"ToolRouter",
|
||||
"ToolInfo",
|
||||
"ToolResult",
|
||||
"AsyncCircuitBreaker",
|
||||
"CircuitState",
|
||||
# Exceptions
|
||||
"MCPError",
|
||||
"MCPConnectionError",
|
||||
"MCPTimeoutError",
|
||||
"MCPToolError",
|
||||
"MCPServerNotFoundError",
|
||||
"MCPToolNotFoundError",
|
||||
"MCPCircuitOpenError",
|
||||
"MCPValidationError",
|
||||
]
|
||||
Reference in New Issue
Block a user