-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp_server.py
More file actions
274 lines (242 loc) · 10.6 KB
/
Copy pathmcp_server.py
File metadata and controls
274 lines (242 loc) · 10.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
from datetime import datetime
from typing import Dict, Any, Optional
from mcp.server.fastmcp import FastMCP
from sqlmodel import Session
from app.db import engine, init_db
from app.models import LLMJob
from app.agents import choose_agent, weather_agent, generate_image_agent, marketing_agent, rag_ingest_agent, rag_query_agent
# 1. Initialize the FastMCP Server
mcp = FastMCP("Agent Orchestrator Server")
# Initialize database on module load to ensure schema exists
init_db()
@mcp.tool()
def submit_agent_task(prompt: str) -> str:
"""
Submits a task to the agent pool (e.g. weather requests, image generation).
Returns the initial response or asks for more information (HITL).
"""
with Session(engine) as session:
# Create a new job in the database
job = LLMJob(prompt=prompt, status="queue")
session.add(job)
session.commit()
session.refresh(job)
# Route the job using the agent chooser
choice = choose_agent(prompt, session=session, user_id="mcp_user")
if choice.action in ("direct_answer", "unsupported"):
job.status = "done"
job.response = choice.direct_response
job.state = {"agent": choice.action}
job.responded_at = datetime.utcnow()
session.add(job)
session.commit()
return f"[Job {job.id} - Done]: {choice.direct_response}"
config = {
"configurable": {
"session": session,
"user_id": "mcp_user"
}
}
try:
if choice.action == "weather_agent":
initial_state = {
"prompt": prompt,
"location": None,
"date": None,
"missing_fields": [],
"next_question": None,
"weather_data": None,
"final_response": None
}
result = weather_agent.invoke(initial_state, config=config)
elif choice.action == "generate_image_agent":
initial_state = {
"prompt": prompt,
"safe_prompt": None,
"is_safe": None,
"rejection_reason": None,
"image_bytes": None,
"image_url": None,
"final_response": None
}
result = generate_image_agent.invoke(initial_state, config=config)
elif choice.action == "marketing_agent":
initial_state = {
"prompt": prompt,
"topic": None,
"drafts": None,
"approved_option": None,
"final_response": None,
"next_question": None
}
result = marketing_agent.invoke(initial_state, config=config)
elif choice.action == "rag_ingest_agent":
initial_state = {
"prompt": prompt,
"url": None,
"title": None,
"scraped_text": None,
"already_exists": None,
"chunk_count": None,
"final_response": None
}
result = rag_ingest_agent.invoke(initial_state, config=config)
elif choice.action == "rag_query_agent":
initial_state = {
"prompt": prompt,
"query_embedding": None,
"retrieved_chunks": None,
"final_response": None
}
result = rag_query_agent.invoke(initial_state, config=config)
else:
# Unsupported action inside this MCP context
job.status = "error"
job.response = f"Unsupported agent: {choice.action}"
session.add(job)
session.commit()
return f"[Job {job.id} - Error]: Unsupported agent: {choice.action}"
# Check if agent requires follow-up input / approval
if result.get("next_question"):
job.status = "awaiting_input"
job.response = result["next_question"]
status_msg = f"[Job {job.id} - Awaiting Input]: {result['next_question']}"
else:
job.status = "done"
job.response = result.get("final_response") or "Done"
job.responded_at = datetime.utcnow()
status_msg = f"[Job {job.id} - Done]: {job.response}"
job.state = {
"agent": choice.action,
**{k: v for k, v in result.items() if k not in ("prompt", "final_response", "next_question", "query_embedding", "scraped_text") and not isinstance(v, bytes)}
}
session.add(job)
session.commit()
return status_msg
except Exception as e:
job.status = "error"
job.response = f"Failed: {str(e)}"
session.add(job)
session.commit()
return f"[Job {job.id} - Error]: {str(e)}"
@mcp.tool()
def submit_followup_answer(job_id: int, answer: str) -> str:
"""
Submits a response/answer to a job that is currently status 'awaiting_input' (requires HITL/additional details).
"""
with Session(engine) as session:
job = session.get(LLMJob, job_id)
if not job:
return f"Error: Job {job_id} not found."
if job.status != "awaiting_input":
return f"Error: Job is in status '{job.status}' and does not expect an answer."
agent_name = job.state.get("agent") if job.state else None
config = {
"configurable": {
"session": session,
"user_id": "mcp_user"
}
}
try:
if agent_name == "weather_agent":
state = {
"prompt": answer,
"location": job.state.get("location"),
"date": job.state.get("date"),
"missing_fields": [],
"next_question": None,
"weather_data": None,
"final_response": None
}
result = weather_agent.invoke(state, config=config)
elif agent_name == "marketing_agent":
state = {
"prompt": answer,
"topic": job.state.get("topic"),
"drafts": job.state.get("drafts"),
"approved_option": job.state.get("approved_option"),
"final_response": None,
"next_question": None
}
result = marketing_agent.invoke(state, config=config)
else:
return f"Error: Agent '{agent_name}' does not support resume/follow-up in this tool."
if result.get("next_question"):
job.status = "awaiting_input"
job.response = result["next_question"]
status_msg = f"[Job {job.id} - Awaiting Input]: {result['next_question']}"
else:
job.status = "done"
job.response = result.get("final_response") or "Done"
job.responded_at = datetime.utcnow()
status_msg = f"[Job {job.id} - Done]: {job.response}"
job.state = {
"agent": agent_name,
**{k: v for k, v in result.items() if k not in ("prompt", "final_response", "next_question") and not isinstance(v, bytes)}
}
session.add(job)
session.commit()
return status_msg
except Exception as e:
return f"Error running agent: {str(e)}"
if __name__ == "__main__":
import sys
# Support running as SSE (http URL) or standard stdio
if len(sys.argv) > 1 and sys.argv[1] == "sse":
import uvicorn
from starlette.routing import Route, Mount
from starlette.responses import Response
base_app = mcp.sse_app()
http_base_app = mcp.streamable_http_app()
original_sse_endpoint = None
message_app = None
http_app = None
for route in base_app.routes:
if isinstance(route, Route) and route.path == "/sse":
original_sse_endpoint = route.endpoint
elif isinstance(route, Mount) and route.path.rstrip("/") == "/messages":
message_app = route.app
for route in http_base_app.routes:
if isinstance(route, Route) and route.path.rstrip("/") == "/mcp":
http_app = route.endpoint
if original_sse_endpoint and message_app and http_app:
class DummyResponse(Response):
async def __call__(self, scope, receive, send) -> None:
pass
async def custom_sse_endpoint(request):
if request.method == "GET":
return await original_sse_endpoint(request)
elif request.method == "POST":
if request.query_params.get("session_id"):
# Forward to standard SSE message handler
await message_app(request.scope, request.receive, request._send)
else:
# Forward to Streamable HTTP handler
await http_app(request.scope, request.receive, request._send)
return DummyResponse()
elif request.method == "DELETE":
return Response("Session closed", status_code=200)
return Response("Method not allowed", status_code=405)
new_routes = []
for route in base_app.routes:
if isinstance(route, Route) and route.path == "/sse":
new_routes.append(
Route(
"/sse",
endpoint=custom_sse_endpoint,
methods=["GET", "POST", "DELETE"]
)
)
else:
new_routes.append(route)
from starlette.applications import Starlette
# Combine the lifespans of both apps
async def combined_lifespan(app):
async with base_app.router.lifespan_context(base_app):
async with http_base_app.router.lifespan_context(http_base_app):
yield
app = Starlette(routes=new_routes, lifespan=combined_lifespan)
print("Starting custom MCP server with POST/DELETE /sse support on http://localhost:8000/sse")
uvicorn.run(app, host="0.0.0.0", port=8000)
else:
mcp.run(transport="stdio")