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.3.0.tar.gz (9.7 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.3.0-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

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

File metadata

File hashes

Hashes for keycardai_mcp_fastmcp-0.3.0.tar.gz
Algorithm Hash digest
SHA256 645ce92b83db7a4ca236ef6600078b55e4560deeaecdffdd5cb9c9dbd60c0c4e
MD5 cceb552673f59ae55300067d3c1799d2
BLAKE2b-256 be2319df8eee5ae35d377f2241032b32efb7b3f930dafa0f6a8c0f196c652c39

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for keycardai_mcp_fastmcp-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e159bd49b0af0ed2dd98ff73b3c98f429afa113090abcca05bc8399b6e16415b
MD5 43f7215148852249caf2918ec52d62a4
BLAKE2b-256 1342b95dfbea1a83b307267c2fe9035ee9a2024c5d397db35657146afef03bd2

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