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

from fastmcp import FastMCP, Context
from keycardai.mcp.integrations.fastmcp import KeycardAuthProvider, OAuthClientMiddleware, get_access_token_for_resource

# Create FastMCP server with KeyCard authentication
mcp = FastMCP("My Secure Service")

# Add KeyCard authentication
auth = KeycardAuthProvider(
    zone_url="https://abc1234.keycard.cloud",
    mcp_server_name="My MCP Service"
)
mcp.set_auth_provider(auth)

# Add OAuth client middleware for token exchange
oauth_middleware = OAuthClientMiddleware(
    zone_url="https://abc1234.keycard.cloud",
    client_name="My MCP Service"
)
mcp.add_middleware(oauth_middleware)

# Use decorator for automatic token exchange
@mcp.tool()
@get_access_token_for_resource("https://www.googleapis.com/calendar/v3")
async def get_calendar_events(ctx: Context, maxResults: int = 10) -> dict:
    # ctx.access_token is automatically available with Google Calendar access
    access_token = ctx.access_token
    # Make API calls with the exchanged token...
    return {"events": [...], "totalEvents": 5}

🏗️ Architecture & Features

This integration package provides FastMCP-specific components for KeyCard OAuth:

Core Components

Component Module Description
KeycardAuthProvider provider.py FastMCP Authentication - Integrates KeyCard zone tokens with FastMCP auth system
OAuthClientMiddleware middleware.py Client Lifecycle - Manages OAuth client initialization and context injection
Token Exchange Decorators decorators.py Automated Exchange - Decorators for seamless resource-specific token exchange

Authentication Flow

  1. Token Verification: KeycardAuthProvider validates incoming JWT tokens using KeyCard zone JWKS
  2. Client Management: OAuthClientMiddleware provides OAuth client to tools via FastMCP context
  3. Token Exchange: @get_access_token_for_resource() decorator automates RFC 8693 token exchange
  4. API Access: Tools receive resource-specific access tokens transparently

Development

This package is part of the KeycardAI Python SDK.

To develop:

# From workspace root
uv sync
uv run --package keycardai-mcp-fastmcp pytest

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.2.0.tar.gz (9.8 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.2.0-py3-none-any.whl (10.2 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for keycardai_mcp_fastmcp-0.2.0.tar.gz
Algorithm Hash digest
SHA256 abb6abcf541912931b7ac16724c5928dc444f19065a923851cbe07036fadb8af
MD5 55f08f5d71e8aaf15d9ffb8dff612fd0
BLAKE2b-256 9388a85eb63f178d8f4455593d0f6915bc2cbf29309fd221b26f9f142748b6b1

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for keycardai_mcp_fastmcp-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 18e2c7a0bfb8c47254dec5e7f1e146fde2c1948c73e51d02929d340f5b02fdb2
MD5 316c3ee29f3b667d8eb99f2d9f144e03
BLAKE2b-256 10157471187afc1ccbad38596ba493a4559bfb9ee20469b8ee228c822ccda3a9

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