Skip to main content

FastMCP integration for KeyCard OAuth client with automated token exchange and authentication

Project description

KeyCard AI FastMCP Integration

A Python package that provides seamless integration between KeyCard and FastMCP servers, enabling secure token exchange and authentication for MCP tools.

Installation

pip install keycardai-mcp-fastmcp

Quick Start

Add KeyCard authentication to your existing FastMCP server:

Install the Package

pip install keycardai-mcp-fastmcp

Get Your KeyCard Zone ID

  1. Sign up at keycard.ai
  2. Navigate to Zone Settings to get your zone ID
  3. Configure your preferred identity provider (Google, Microsoft, etc.)
  4. Create an MCP resource in your zone

Add Authentication to Your FastMCP Server

from fastmcp import FastMCP, Context
from keycardai.mcp.integrations.fastmcp import AuthProvider

# Configure KeyCard authentication (recommended: use zone_id)
auth_provider = AuthProvider(
    zone_id="your-zone-id",  # Get this from keycard.ai
    mcp_server_name="My Secure FastMCP Server",
    mcp_server_url="http://127.0.0.1:8000/"
)

# Get the RemoteAuthProvider for FastMCP
auth = auth_provider.get_remote_auth_provider()

# Create authenticated FastMCP server
mcp = FastMCP("My Secure FastMCP Server", auth=auth)

@mcp.tool()
def hello_world(name: str) -> str:
    return f"Hello, {name}!"

# Example with token exchange for external API access
@mcp.tool()
@auth_provider.grant("https://api.example.com")
def call_external_api(ctx: Context, query: str) -> str:
    # Access delegated token through context namespace
    token = ctx.get_state("keycardai").access("https://api.example.com").access_token
    # Use token to call external API
    return f"Results for {query}"

if __name__ == "__main__":
    mcp.run(transport="streamable-http")

🎉 Your FastMCP server is now protected with KeyCard authentication! 🎉

Examples

For complete examples and advanced usage patterns, see our documentation.

License

MIT License - see LICENSE file for details.

Support

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

keycardai_mcp_fastmcp-0.4.1.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

keycardai_mcp_fastmcp-0.4.1-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file keycardai_mcp_fastmcp-0.4.1.tar.gz.

File metadata

File hashes

Hashes for keycardai_mcp_fastmcp-0.4.1.tar.gz
Algorithm Hash digest
SHA256 4449931d6ae6ecf58708e6d502002c812a405266ec9333b91cfaa58e854ab866
MD5 ae25f0463a705d7860229e2ad29b674e
BLAKE2b-256 a50c280628e37d35de039c41813d72bf51bbc0153b827628a32ffb8a097b9d0b

See more details on using hashes here.

File details

Details for the file keycardai_mcp_fastmcp-0.4.1-py3-none-any.whl.

File metadata

File hashes

Hashes for keycardai_mcp_fastmcp-0.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9189f4a0e89f93fd83e5222311e7cbdfe9e4ec83200471623136bbe1d9fb7ce2
MD5 f354779f7515fb05e087255f05759a10
BLAKE2b-256 66b16084e69d3de3688f769cf1e38cab32969e5cdb4bac3358e9718783e75139

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page