|
13 | 13 | # limitations under the License. |
14 | 14 |
|
15 | 15 | import os |
16 | | - |
17 | 16 | from agent import agent, app_name, short_term_memory |
18 | 17 | from veadk.a2a.ve_a2a_server import init_app |
19 | 18 | from veadk.tracing.base_tracer import BaseTracer |
20 | 19 | from veadk.tracing.telemetry.opentelemetry_tracer import OpentelemetryTracer |
| 20 | +from veadk.runner import Runner |
| 21 | +from contextlib import asynccontextmanager |
| 22 | +from fastmcp import FastMCP |
| 23 | +from fastapi import FastAPI |
21 | 24 |
|
22 | 25 |
|
23 | 26 | # ============================================================================== |
|
62 | 65 | # Tracer Config ================================================================ |
63 | 66 | # ============================================================================== |
64 | 67 |
|
65 | | -app = init_app( |
66 | | - server_url="0.0.0.0", # Automatic identification is not supported yet. |
| 68 | +# Create A2A app |
| 69 | +a2a_app = init_app( |
| 70 | + server_url="0.0.0.0", |
67 | 71 | app_name=app_name, |
68 | 72 | agent=agent, |
69 | 73 | short_term_memory=short_term_memory, |
70 | 74 | ) |
| 75 | + |
| 76 | +# Add a2a app to fastmcp |
| 77 | +runner = Runner( |
| 78 | + agent=agent, |
| 79 | + short_term_memory=short_term_memory, |
| 80 | + app_name=app_name, |
| 81 | + user_id="", |
| 82 | +) |
| 83 | + |
| 84 | + |
| 85 | +# mcp server |
| 86 | +@a2a_app.post("/run_agent", operation_id="run_agent", tags=["mcp"]) |
| 87 | +async def run_agent( |
| 88 | + user_input: str, |
| 89 | + user_id: str = "unknown_user", |
| 90 | + session_id: str = "unknown_session", |
| 91 | +) -> str: |
| 92 | + """ |
| 93 | + Execute agent with user input and return final output |
| 94 | + Args: |
| 95 | + user_input: User's input message |
| 96 | + user_id: User identifier |
| 97 | + session_id: Session identifier |
| 98 | + Returns: |
| 99 | + Final agent response |
| 100 | + """ |
| 101 | + # Set user_id for runner |
| 102 | + runner.user_id = user_id |
| 103 | + |
| 104 | + # Running agent and get final output |
| 105 | + final_output = await runner.run( |
| 106 | + messages=user_input, |
| 107 | + session_id=session_id, |
| 108 | + ) |
| 109 | + return final_output |
| 110 | + |
| 111 | + |
| 112 | +mcp = FastMCP.from_fastapi(app=a2a_app, name=app_name, include_tags={"mcp"}) |
| 113 | + |
| 114 | +# Create MCP ASGI app |
| 115 | +mcp_app = mcp.http_app(path="/") |
| 116 | + |
| 117 | + |
| 118 | +# Combined lifespan management |
| 119 | +@asynccontextmanager |
| 120 | +async def combined_lifespan(app: FastAPI): |
| 121 | + async with mcp_app.lifespan(app): |
| 122 | + yield |
| 123 | + |
| 124 | + |
| 125 | +# Create main FastAPI app with combined lifespan |
| 126 | +app = FastAPI(title=a2a_app.title, version=a2a_app.version, lifespan=combined_lifespan) |
| 127 | + |
| 128 | +# Mount A2A routes to main app |
| 129 | +for route in a2a_app.routes: |
| 130 | + app.routes.append(route) |
| 131 | + |
| 132 | +# Mount MCP server at /mcp endpoint |
| 133 | +app.mount("/mcp", mcp_app) |
0 commit comments