from anyio import to_thread from starlette.applications import Starlette from starlette.responses import HTMLResponse, JSONResponse from starlette.routing import Route from smolagents import CodeAgent, InferenceClientModel, MCPClient # Create an MCP client to connect to the MCP server mcp_server_parameters = { "url": "https://evalstate-hf-mcp-server.hf.space/mcp", "transport": "streamable-http", } mcp_client = MCPClient(server_parameters=mcp_server_parameters) # Create a CodeAgent with a specific model and the tools from the MCP client agent = CodeAgent( model=InferenceClientModel(model_id="Qwen/Qwen2.5-Coder-32B-Instruct"), tools=mcp_client.get_tools(), ) # Define the shutdown handler to disconnect the MCP client async def shutdown(): mcp_client.disconnect() async def homepage(request): return HTMLResponse( r""" Smolagents Demo

🤖 Smolagents Demo

Hello! I'm a code agent with access to MCP tools. Ask me anything!
""" ) async def chat(request): data = await request.json() message = data.get("message", "").strip() # Run in a thread to avoid blocking the event loop result = await to_thread.run_sync(agent.run, message) # Format the result if it's a complex data structure reply = str(result) return JSONResponse({"reply": reply}) app = Starlette( debug=True, routes=[ Route("/", homepage), Route("/chat", chat, methods=["POST"]), ], on_shutdown=[shutdown], # Register the shutdown handler: disconnect the MCP client )