|
| 1 | +#!/usr/bin/env python3 |
| 2 | +""" |
| 3 | +MCP Auth Test Server - Conformance Test Server with Authentication |
| 4 | +
|
| 5 | +A minimal MCP server that requires Bearer token authentication. |
| 6 | +This server is used for testing OAuth authentication flows in conformance tests. |
| 7 | +
|
| 8 | +Required environment variables: |
| 9 | +- MCP_CONFORMANCE_AUTH_SERVER_URL: URL of the authorization server |
| 10 | +
|
| 11 | +Optional environment variables: |
| 12 | +- PORT: Server port (default: 3001) |
| 13 | +""" |
| 14 | + |
| 15 | +import logging |
| 16 | +import os |
| 17 | +import sys |
| 18 | + |
| 19 | +import click |
| 20 | +from mcp.server.auth.provider import AccessToken, TokenVerifier |
| 21 | +from mcp.server.auth.settings import AuthSettings |
| 22 | +from mcp.server.fastmcp import FastMCP |
| 23 | +from pydantic import AnyHttpUrl |
| 24 | + |
| 25 | +logger = logging.getLogger(__name__) |
| 26 | + |
| 27 | + |
| 28 | +class ConformanceTokenVerifier(TokenVerifier): |
| 29 | + """ |
| 30 | + Token verifier for conformance testing. |
| 31 | +
|
| 32 | + Validates Bearer tokens that start with 'test-token' or 'cc-token' |
| 33 | + (as issued by the fake auth server). |
| 34 | + """ |
| 35 | + |
| 36 | + async def verify_token(self, token: str) -> AccessToken | None: |
| 37 | + """Verify a bearer token and return access info if valid.""" |
| 38 | + # Accept tokens that start with 'test-token' or 'cc-token' |
| 39 | + if token.startswith("test-token") or token.startswith("cc-token"): |
| 40 | + return AccessToken( |
| 41 | + token=token, |
| 42 | + client_id="conformance-test-client", |
| 43 | + scopes=["mcp:read", "mcp:write"], |
| 44 | + ) |
| 45 | + return None |
| 46 | + |
| 47 | + |
| 48 | +def create_server(auth_server_url: str, port: int) -> FastMCP: |
| 49 | + """Create and configure the MCP auth test server.""" |
| 50 | + base_url = f"http://localhost:{port}" |
| 51 | + |
| 52 | + mcp = FastMCP( |
| 53 | + name="mcp-auth-test-server", |
| 54 | + token_verifier=ConformanceTokenVerifier(), |
| 55 | + auth=AuthSettings( |
| 56 | + issuer_url=AnyHttpUrl(auth_server_url), |
| 57 | + resource_server_url=AnyHttpUrl(base_url), |
| 58 | + required_scopes=[], # No specific scopes required for conformance tests |
| 59 | + ), |
| 60 | + json_response=True, |
| 61 | + port=port, |
| 62 | + ) |
| 63 | + |
| 64 | + @mcp.tool() |
| 65 | + def echo(message: str = "No message provided") -> str: |
| 66 | + """Echoes back the provided message - used for testing authenticated calls.""" |
| 67 | + return f"Echo: {message}" |
| 68 | + |
| 69 | + @mcp.tool(name="test-tool") |
| 70 | + def test_tool() -> str: |
| 71 | + """A simple test tool that returns a success message.""" |
| 72 | + return "test" |
| 73 | + |
| 74 | + return mcp |
| 75 | + |
| 76 | + |
| 77 | +@click.command() |
| 78 | +@click.option("--port", default=None, type=int, help="Port to listen on for HTTP") |
| 79 | +@click.option( |
| 80 | + "--log-level", |
| 81 | + default="INFO", |
| 82 | + help="Logging level (DEBUG, INFO, WARNING, ERROR, CRITICAL)", |
| 83 | +) |
| 84 | +def main(port: int | None, log_level: str) -> int: |
| 85 | + """Run the MCP Auth Test Server.""" |
| 86 | + logging.basicConfig( |
| 87 | + level=getattr(logging, log_level.upper()), |
| 88 | + format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", |
| 89 | + ) |
| 90 | + |
| 91 | + # Check for required environment variable |
| 92 | + auth_server_url = os.environ.get("MCP_CONFORMANCE_AUTH_SERVER_URL") |
| 93 | + if not auth_server_url: |
| 94 | + logger.error("Error: MCP_CONFORMANCE_AUTH_SERVER_URL environment variable is required") |
| 95 | + logger.error( |
| 96 | + "Usage: MCP_CONFORMANCE_AUTH_SERVER_URL=http://localhost:3000 python -m mcp_conformance_auth_server" |
| 97 | + ) |
| 98 | + sys.exit(1) |
| 99 | + |
| 100 | + # Get port from argument or environment |
| 101 | + if port is None: |
| 102 | + port = int(os.environ.get("PORT", "3001")) |
| 103 | + |
| 104 | + logger.info(f"Starting MCP Auth Test Server on port {port}") |
| 105 | + logger.info(f"Endpoint will be: http://localhost:{port}/mcp") |
| 106 | + logger.info(f"PRM endpoint: http://localhost:{port}/.well-known/oauth-protected-resource") |
| 107 | + logger.info(f"Auth server: {auth_server_url}") |
| 108 | + |
| 109 | + mcp = create_server(auth_server_url, port) |
| 110 | + mcp.run(transport="streamable-http") |
| 111 | + |
| 112 | + return 0 |
| 113 | + |
| 114 | + |
| 115 | +if __name__ == "__main__": |
| 116 | + main() |
0 commit comments