|
| 1 | +""" |
| 2 | +Demonstration of an MCP agent server configured with OAuth. |
| 3 | +""" |
| 4 | + |
| 5 | +import asyncio |
| 6 | +from typing import Optional |
| 7 | +from pydantic import AnyHttpUrl |
| 8 | + |
| 9 | +from mcp_agent.core.context import Context as AppContext |
| 10 | + |
| 11 | +from mcp_agent.app import MCPApp |
| 12 | +from mcp_agent.server.app_server import create_mcp_server_for_app |
| 13 | +from mcp_agent.config import Settings, LoggerSettings, \ |
| 14 | + OAuthTokenStoreSettings, OAuthSettings, MCPAuthorizationServerSettings |
| 15 | + |
| 16 | + |
| 17 | +auth_server = "https://auth.mcp-agent.com" # the MCP Agent Cloud auth server, or replace with your own |
| 18 | +resource_server = "http://localhost:8000" # This server's URL |
| 19 | + |
| 20 | +client_id = "<client id from registration.py>" |
| 21 | +client_secret = "<client secret from registration.py>" |
| 22 | + |
| 23 | +settings = Settings( |
| 24 | + execution_engine="asyncio", |
| 25 | + logger=LoggerSettings(level="info"), |
| 26 | + authorization=MCPAuthorizationServerSettings( |
| 27 | + enabled=True, |
| 28 | + issuer_url=AnyHttpUrl(auth_server), |
| 29 | + resource_server_url=AnyHttpUrl(resource_server), |
| 30 | + client_id=client_id, |
| 31 | + client_secret=client_secret, |
| 32 | + required_scopes=["mcp"], |
| 33 | + expected_audiences=[client_id], |
| 34 | + ), |
| 35 | + oauth=OAuthSettings( |
| 36 | + callback_base_url=AnyHttpUrl(resource_server), |
| 37 | + flow_timeout_seconds=300, |
| 38 | + token_store=OAuthTokenStoreSettings(refresh_leeway_seconds=60), |
| 39 | + ) |
| 40 | + ) |
| 41 | + |
| 42 | + |
| 43 | +# Define the MCPApp instance. The server created for this app will advertise the |
| 44 | +# MCP logging capability and forward structured logs upstream to connected clients. |
| 45 | +app = MCPApp( |
| 46 | + name="oauth_demo", |
| 47 | + description="Basic agent server example", |
| 48 | + settings=settings, |
| 49 | +) |
| 50 | + |
| 51 | + |
| 52 | +@app.tool(name="hello_world") |
| 53 | +async def hello(app_ctx: Optional[AppContext] = None) -> str: |
| 54 | + # Use the context's app if available for proper logging with upstream_session |
| 55 | + _app = app_ctx.app if app_ctx else app |
| 56 | + # Ensure the app's logger is bound to the current context with upstream_session |
| 57 | + if _app._logger and hasattr(_app._logger, "_bound_context"): |
| 58 | + _app._logger._bound_context = app_ctx |
| 59 | + |
| 60 | + if app_ctx.current_user: |
| 61 | + user = app_ctx.current_user |
| 62 | + if user.claims and "username" in user.claims: |
| 63 | + return f"Hello, {user.claims['username']}!" |
| 64 | + else: |
| 65 | + return f"Hello, user with ID {user.subject}!" |
| 66 | + else: |
| 67 | + return "Hello, anonymous user!" |
| 68 | + |
| 69 | +async def main(): |
| 70 | + async with app.run() as agent_app: |
| 71 | + # Log registered workflows and agent configurations |
| 72 | + agent_app.logger.info(f"Creating MCP server for {agent_app.name}") |
| 73 | + |
| 74 | + agent_app.logger.info("Registered workflows:") |
| 75 | + for workflow_id in agent_app.workflows: |
| 76 | + agent_app.logger.info(f" - {workflow_id}") |
| 77 | + |
| 78 | + # Create the MCP server that exposes both workflows and agent configurations, |
| 79 | + # optionally using custom FastMCP settings |
| 80 | + mcp_server = create_mcp_server_for_app(agent_app) |
| 81 | + agent_app.logger.info(f"MCP Server settings: {mcp_server.settings}") |
| 82 | + |
| 83 | + # Run the server |
| 84 | + await mcp_server.run_sse_async() |
| 85 | + |
| 86 | + |
| 87 | +if __name__ == "__main__": |
| 88 | + asyncio.run(main()) |
0 commit comments