-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.py
More file actions
88 lines (73 loc) · 2.86 KB
/
server.py
File metadata and controls
88 lines (73 loc) · 2.86 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
86
87
88
from fastmcp import FastMCP, Client
from fastmcp.client.transports import NpxStdioTransport
import structlog
import os
import json
import asyncio
import time
from agent.common.config import (
NOTION_TOKEN,
GOOGLE_MAPS_API_KEY
)
logger = structlog.get_logger()
def setup_mcp_proxy_servers():
"""
Setup proxy servers for various MCP services.
Returns the host and port information for the SSE server.
"""
log = logger.bind()
# Create a composite FastMCP server
composite_mcp = FastMCP("CompositeServer")
# Setup function for each service in a thread
async def _run_proxy():
# Define services with their configurations
services = {
"notion": {
"package": "@notionhq/notion-mcp-server",
"args": [],
"env": {
"OPENAPI_MCP_HEADERS": json.dumps({
"Authorization": f"Bearer {NOTION_TOKEN}",
"Notion-Version": "2022-06-28"
})
}
},
"google-maps": {
"package": "@modelcontextprotocol/server-google-maps",
"args": [],
"env": {
"GOOGLE_MAPS_API_KEY": GOOGLE_MAPS_API_KEY
}
}
}
# Initialize each service and mount to the composite server
for service_name, config in services.items():
try:
# Create transport with environment variables
transport = NpxStdioTransport(
package=config["package"],
args=config["args"],
env_vars=config["env"]
)
# Create client with explicit transport
stdio_client = Client(transport)
# Create a proxy server for this service
service_mcp = FastMCP.from_client(stdio_client, name=f"{service_name.capitalize()}Server")
# Mount the service to the composite server
composite_mcp.mount(service_name, service_mcp)
log.info(f"Mounted {service_name} MCP service")
except Exception as e:
log.error(f"Failed to initialize {service_name} service", error=str(e))
# Run the composite server with SSE transport
host = "127.0.0.1"
port = 8000
await composite_mcp.run_async(transport="sse", host=host, port=port)
# Start the proxy server in a separate thread
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(_run_proxy())
# Give the server a moment to start
time.sleep(2)
log.info("MCP proxy server started on http://127.0.0.1:8000/sse")
if __name__ == "__main__":
setup_mcp_proxy_servers()