-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathmcp_server.py
More file actions
85 lines (70 loc) · 2.81 KB
/
mcp_server.py
File metadata and controls
85 lines (70 loc) · 2.81 KB
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
"""
MCP (Model Context Protocol) integration for Feast Feature Server.
This module provides MCP support for Feast by integrating with fastapi_mcp
to expose Feast functionality through the Model Context Protocol.
"""
import logging
from typing import Optional
from feast.feature_store import FeatureStore
logger = logging.getLogger(__name__)
try:
from fastapi_mcp import FastApiMCP
MCP_AVAILABLE = True
except ImportError:
logger.warning(
"fastapi_mcp is not installed. MCP support will be disabled. "
"Install it with: pip install fastapi_mcp"
)
MCP_AVAILABLE = False
# Create placeholder classes for testing
FastApiMCP = None
class McpTransportNotSupportedError(RuntimeError):
pass
def add_mcp_support_to_app(app, store: FeatureStore, config) -> Optional["FastApiMCP"]:
"""Add MCP support to the FastAPI app if enabled in configuration."""
if not MCP_AVAILABLE:
logger.warning("MCP support requested but fastapi_mcp is not available")
return None
try:
# Create MCP server from the FastAPI app
mcp = FastApiMCP(
app,
name=getattr(config, "mcp_server_name", "feast-feature-store"),
description="Feast Feature Store MCP Server - Access feature store data and operations through MCP",
)
transport = getattr(config, "mcp_transport", "sse")
if transport == "http":
mount_http = getattr(mcp, "mount_http", None)
if mount_http is None:
raise McpTransportNotSupportedError(
"mcp_transport=http requires fastapi_mcp with FastApiMCP.mount_http(). "
"Upgrade fastapi_mcp (or install feast[mcp]) to a newer version."
)
mount_http()
elif transport == "sse":
mount_sse = getattr(mcp, "mount_sse", None)
if mount_sse is not None:
mount_sse()
else:
logger.warning(
"transport sse not supported, fallback to the deprecated mount()."
)
mcp.mount()
else:
# Defensive guard for programmatic callers.
raise McpTransportNotSupportedError(
f"Unsupported mcp_transport={transport!r}. Expected 'sse' or 'http'."
)
logger.info(
"MCP support has been enabled for the Feast feature server at /mcp endpoint"
)
logger.info(
f"MCP integration initialized for {getattr(config, 'mcp_server_name', 'feast-feature-store')} "
f"v{getattr(config, 'mcp_server_version', '1.0.0')}"
)
return mcp
except McpTransportNotSupportedError:
raise
except Exception as e:
logger.error(f"Failed to initialize MCP integration: {e}", exc_info=True)
return None