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.0.tar.gz (8.9 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.0-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for keycardai_mcp_fastmcp-0.4.0.tar.gz
Algorithm Hash digest
SHA256 2bb26579c11fc7f555ea532aad462860aadb925b65a27b5fb9edde7f0965f496
MD5 6d0913f17bbc9c3ca6b10671652f5da1
BLAKE2b-256 f47c6a97a59ff656fbabc30b2288668cf1b1c7f71343d26a4791ff9a06a47bc9

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for keycardai_mcp_fastmcp-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 83d67af1bca5b048764df80d4691ef37a76e2ba58247ba26909daea24d0f5282
MD5 43da9948bec1d54938d0cba57102c058
BLAKE2b-256 ee1690838079c1b661cb8f13185707e4d067da1da66fa5a8cb95b67fd3596474

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