from fastapi import FastAPI
from pydantic import BaseModel
from registry.agent_registry import AGENT_REGISTRY
import logging
# Configure logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
# Create the FastAPI app instance
app = FastAPI()
class TaskPayload(BaseModel):
"""
Request body model containing:
- agent: name of the agent to route to.
- input: input text to be processed.
"""
agent: str
input: str
class TaskResponse(BaseModel):
"""
Response model for agent execution:
- status: success or error.
- agent_output: optional, returned by the agent on success.
- message: optional, returned on error.
"""
status: str
agent_output: str | None = None
message: str | None = None
@app.post("/handle_task", response_model=TaskResponse)
async def handle_task_request(payload: TaskPayload):
"""
Handle task request by routing input to the specified agent.
Args:
payload (TaskPayload): Contains agent name and input.
Returns:
TaskResponse: Result of agent processing or error message.
"""
agent_name = payload.agent
input_text = payload.input
logger.info(f"[MCP_BRIDGE] Received request for agent '{agent_name}' with input: {input_text}")
agent = AGENT_REGISTRY.get(agent_name)
if agent:
try:
result = agent.run(input_text)
logger.info(f"[MCP_BRIDGE] Agent '{agent_name}' executed successfully.")
return TaskResponse(status="success", agent_output=result)
except Exception as e:
logger.error(f"[MCP_BRIDGE] Agent '{agent_name}' failed: {e}")
return TaskResponse(status="error", message=str(e))
else:
logger.warning(f"[MCP_BRIDGE] Agent '{agent_name}' not found.")
return TaskResponse(status="error", message=f"Agent '{agent_name}' not found.")