-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
123 lines (97 loc) · 4.22 KB
/
main.py
File metadata and controls
123 lines (97 loc) · 4.22 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# -*- coding: utf-8 -*-
import sys
import httpx
import anyio
from mcp.server.fastmcp import FastMCP, Image, Context
import base64
from typing import Optional, Dict, Any, Union
# Create a generic MCP server for interacting with Revit
# Use stateless_http=True and json_response=True for better compatibility
mcp = FastMCP(
"Revit MCP Server",
host="127.0.0.1",
port=8000,
stateless_http=True,
json_response=True
)
# Configuration
REVIT_HOST = "localhost"
REVIT_PORT = 48884 # Default pyRevit Routes port
BASE_URL = f"http://{REVIT_HOST}:{REVIT_PORT}/revit_mcp"
async def revit_get(endpoint: str, ctx: Context = None, **kwargs) -> Union[Dict, str]:
"""Simple GET request to Revit API"""
return await _revit_call("GET", endpoint, ctx=ctx, **kwargs)
async def revit_post(endpoint: str, data: Dict[str, Any], ctx: Context = None, **kwargs) -> Union[Dict, str]:
"""Simple POST request to Revit API"""
return await _revit_call("POST", endpoint, data=data, ctx=ctx, **kwargs)
async def revit_image(endpoint: str, ctx: Context = None) -> Union[Image, str]:
"""GET request that returns an Image object"""
try:
async with httpx.AsyncClient(timeout=60.0) as client:
response = await client.get(f"{BASE_URL}{endpoint}")
if response.status_code == 200:
data = response.json()
image_bytes = base64.b64decode(data["image_data"])
return Image(data=image_bytes, format="png")
else:
return f"Error: {response.status_code} - {response.text}"
except httpx.TimeoutException:
return "Error: Image export timed out after 60 seconds."
except Exception as e:
msg = str(e) or type(e).__name__
return f"Error: {msg}"
async def _revit_call(method: str, endpoint: str, data: Dict = None, ctx: Context = None,
timeout: float = 30.0, params: Dict = None) -> Union[Dict, str]:
"""Internal function handling all HTTP calls"""
try:
async with httpx.AsyncClient(timeout=timeout) as client:
url = f"{BASE_URL}{endpoint}"
if method == "GET":
response = await client.get(url, params=params)
else: # POST
response = await client.post(url, json=data, headers={"Content-Type": "application/json"})
return response.json() if response.status_code == 200 else f"Error: {response.status_code} - {response.text}"
except httpx.TimeoutException:
return f"Error: Request timed out after {timeout} seconds. The operation may still be running in Revit."
except Exception as e:
msg = str(e) or type(e).__name__
return f"Error: {msg}"
# Register all tools BEFORE the main block
from tools import register_tools
register_tools(mcp, revit_get, revit_post, revit_image)
async def run_combined_async():
"""Run server with both SSE and streamable-http endpoints.
This allows clients to connect via either:
- SSE: GET /sse, POST /messages/
- Streamable-HTTP: POST/GET /mcp
"""
import uvicorn
# Get the streamable-http app first - it has the proper lifespan
# that initializes the session manager's task group
http_app = mcp.streamable_http_app()
# Get SSE routes (SSE doesn't need special lifespan - it creates
# task groups per-request in connect_sse())
sse_app = mcp.sse_app()
# Add SSE routes to the http app (preserving its lifespan)
for route in sse_app.routes:
http_app.routes.append(route)
config = uvicorn.Config(
http_app,
host=mcp.settings.host,
port=mcp.settings.port,
log_level=mcp.settings.log_level.lower(),
)
server = uvicorn.Server(config)
await server.serve()
if __name__ == "__main__":
transport = "stdio"
if "--sse" in sys.argv:
transport = "sse"
elif "--http" in sys.argv or "--streamable-http" in sys.argv:
transport = "streamable-http"
elif "--combined" in sys.argv:
# Run both SSE and streamable-http transports simultaneously
print("Starting combined server with SSE (/sse, /messages/) and streamable-http (/mcp) endpoints...")
anyio.run(run_combined_async)
sys.exit(0)
mcp.run(transport=transport)