Skip to content

Add authentication support for MCP servers #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

elijahdev0
Copy link
Owner

This PR implements authentication support for FastAPI-MCP as discussed in issue tadata-org#10.

Features added:

  • Authentication module with support for:
    • Bearer token authentication
    • API key authentication (in header or query)
    • Custom authentication functions
  • Added auth_config parameter to add_mcp_server and mount_mcp_server functions
  • Authentication is applied to both the SSE connection endpoint and message endpoint
  • Updated documentation in README
  • Added examples:
    • authenticated_server.py - JWT authentication example
    • simple_bearer_auth.py - Simple bearer token example

This implementation follows the MCP authentication specification, which recommends using bearer tokens in the Authorization header.

How to use:

from fastapi import FastAPI
from fastapi_mcp import add_mcp_server, AuthConfig

app = FastAPI()

# Configure authentication
auth_config = AuthConfig(
    enabled=True,
    bearer_token="your-secret-token"
)

# Add MCP server with authentication
mcp_server = add_mcp_server(
    app,
    mount_path="/mcp",
    auth_config=auth_config
)

For more complex authentication scenarios, custom authentication functions are supported.

Fixes tadata-org#10

@shahar4499
Copy link

@elijahdev0 Hey, thank you! Looks promising. I will review this PR, but you need to open it in the original repo for it to be mergeable. Right now it's open only in your fork :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support Authentication According to Current MCP Spec
2 participants