forked from CityOfBoston/OpenContext
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlocal_server.py
More file actions
101 lines (77 loc) · 2.6 KB
/
Copy pathlocal_server.py
File metadata and controls
101 lines (77 loc) · 2.6 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
89
90
91
92
93
94
95
96
97
98
99
100
101
# run_local_server.py
"""Run OpenContext MCP server locally for testing (no Lambda needed)."""
import asyncio
import json
from pathlib import Path
import yaml
from aiohttp import web
from core.plugin_manager import PluginManager
from core.mcp_server import MCPServer
# Load config
with open("config.yaml") as f:
config = yaml.safe_load(f)
# Global server instance
_plugin_manager = None
_mcp_server = None
async def init_server():
"""Initialize server on startup."""
global _plugin_manager, _mcp_server
print("🚀 Initializing OpenContext MCP Server locally...")
# Initialize Plugin Manager
_plugin_manager = PluginManager(config)
await _plugin_manager.load_plugins()
# Initialize MCP Server
_mcp_server = MCPServer(_plugin_manager)
print("✅ Server initialized successfully")
print(f"Loaded plugins: {list(_plugin_manager.plugins.keys())}")
print(f"Available tools: {len(_plugin_manager.get_all_tools())}")
async def handle_mcp_request(request):
"""Handle MCP JSON-RPC request."""
try:
body = await request.text()
headers = dict(request.headers)
# Use the same handler as Lambda
response = await _mcp_server.handle_http_request(body, headers)
return web.Response(
text=response.get("body", "{}"),
status=response.get("statusCode", 200),
headers=response.get("headers", {}),
)
except Exception as e:
return web.Response(
text=json.dumps(
{
"jsonrpc": "2.0",
"id": None,
"error": {"code": -32603, "message": str(e)},
}
),
status=500,
headers={"Content-Type": "application/json"},
)
async def start_server():
"""Start local HTTP server."""
await init_server()
app = web.Application()
app.router.add_post("/", handle_mcp_request)
app.router.add_post("/mcp", handle_mcp_request)
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, "localhost", 8000)
await site.start()
print("\n" + "=" * 50)
print("🌐 Local MCP Server running!")
print("=" * 50)
print(f"URL: http://localhost:8000")
print("\nTest with:")
print(" opencontext-client http://localhost:8000")
print("\nPress Ctrl+C to stop")
print("=" * 50 + "\n")
# Keep running
try:
await asyncio.Event().wait()
except KeyboardInterrupt:
print("\n👋 Shutting down...")
await _plugin_manager.shutdown()
if __name__ == "__main__":
asyncio.run(start_server())