Skip to main content

A Python SDK for Model Context Protocol (MCP) functionality with simplified authentication and authorization

Project description

KeyCard AI MCP SDK

A comprehensive Python SDK for Model Context Protocol (MCP) functionality that simplifies authentication and authorization concerns for developers working with AI/LLM integrations.

Installation

pip install keycardai-mcp

Quick Start

from keycardai.mcp import *

# MCP Server with authentication
server = MCPServer(
    name="my-mcp-server",
    version="1.0.0",
    auth_config=MCPAuthConfig(
        oauth_client_id="your_client_id",
        oauth_client_secret="your_client_secret"
    )
)

# Register authenticated resources
@server.resource("user-data")
async def get_user_data(context: MCPContext) -> MCPResource:
    # Automatic token validation and user context
    user = context.authenticated_user
    return MCPResource(
        uri=f"user://{user.id}/data",
        content=await fetch_user_data(user.id)
    )

# MCP Client with token management
client = MCPClient(
    server_url="https://api.example.com/mcp",
    auth=MCPOAuthAuth(
        client_id="client_id",
        client_secret="client_secret"
    )
)

# Access authenticated resources
user_data = await client.get_resource("user-data")

🏗️ Architecture & Features

This SDK provides comprehensive MCP functionality with enterprise-grade security:

Core MCP Components

Component Module Description
MCP Server server.py Authenticated MCP Server - Host MCP resources with built-in OAuth 2.0 authentication
MCP Client client.py Secure MCP Client - Connect to MCP servers with automatic token management
Resource Management resources.py Authenticated Resources - Secure resource access with user context
Tool Integration tools.py Secure Tools - Execute MCP tools with proper authorization

Authentication & Security

Feature Module Description
OAuth 2.0 Integration auth.py Token Management - Seamless OAuth integration for MCP operations
Token Validation validation.py Security Middleware - Automatic token validation and user context
Scope Management scopes.py Permission Control - Fine-grained access control for MCP resources
Session Management sessions.py Secure Sessions - Persistent authenticated sessions for MCP clients

MCP Protocol Extensions

Standard Module Description
Resource Templates templates.py Dynamic Resources - Template-based resource generation with auth context
Prompt Security prompts.py Secure Prompts - User-aware prompt templates and execution
Tool Authorization tools.py Permission Checks - Role-based access control for MCP tools
Logging & Monitoring monitoring.py Security Audit - Comprehensive logging of authenticated operations

Features

  • MCP Protocol Compliance: Full implementation of Model Context Protocol standards
  • OAuth 2.0 Integration: Seamless authentication with industry-standard OAuth flows
  • Type Safe: Full type hints with Pydantic models for all MCP operations
  • Async Support: Native async/await support for all MCP operations
  • Enterprise Security: Token validation, scope management, and audit logging
  • Developer Friendly: Simplified API that abstracts away authentication complexity
  • Production Ready: Battle-tested security patterns and comprehensive error handling

Use Cases

🤖 AI Agent Platforms

# Secure MCP server for AI agents
server = MCPServer(auth_required=True)

@server.tool("execute-query")
async def execute_query(context: MCPContext, query: str) -> dict:
    # Only authenticated users with 'query:execute' scope
    if not context.has_scope("query:execute"):
        raise MCPAuthError("Insufficient permissions")
    
    return await database.execute(query, user=context.user)

🔐 Enterprise LLM Integration

# Corporate LLM with secure resource access
client = MCPClient(
    server_url="https://corp-llm.company.com/mcp",
    auth=MCPOAuthAuth.from_client_credentials(
        client_id="corp-client",
        client_secret="secret",
        scopes=["documents:read", "calendar:read"]
    )
)

# Access corporate resources securely
documents = await client.list_resources("documents")

🌐 Multi-Tenant SaaS

# Tenant-aware MCP resources
@server.resource("tenant-data")
async def get_tenant_data(context: MCPContext) -> MCPResource:
    tenant_id = context.user.tenant_id
    return await fetch_tenant_data(tenant_id, user=context.user)

Security Best Practices

Token Management

  • Automatic token refresh and rotation
  • Secure token storage with encryption
  • Scope-based permission validation
  • Session timeout and cleanup

Authentication Flows

  • Authorization Code flow for web applications
  • Client Credentials flow for service-to-service
  • Device Code flow for CLI applications
  • PKCE for public clients

Monitoring & Compliance

  • Comprehensive audit logging
  • Rate limiting and abuse prevention
  • GDPR-compliant user data handling
  • SOC 2 security controls

Development

This package is part of the KeycardAI Python SDK workspace.

To develop:

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

Examples

See the examples directory for comprehensive examples including:

  • Basic MCP server setup
  • OAuth integration patterns
  • Multi-tenant configurations
  • Enterprise deployment guides

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-0.4.0.tar.gz (26.4 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-0.4.0-py3-none-any.whl (19.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: keycardai_mcp-0.4.0.tar.gz
  • Upload date:
  • Size: 26.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.8.18

File hashes

Hashes for keycardai_mcp-0.4.0.tar.gz
Algorithm Hash digest
SHA256 7ec2fade693c470e6dd1d1361677916deb7341b9d556de7595cb34681414a3c0
MD5 8737bd068ada52e919f243594bf28565
BLAKE2b-256 6b6b203199f4cb593445ca1c891467352cdcba1b83b80abf2b5c6d08d09726be

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for keycardai_mcp-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cda4b02cf89916514e2cfd752fcadf81857b1d6c73cc647891a73dae08725fd1
MD5 eff887cc87181602e7d11180256f6153
BLAKE2b-256 fe57faaa62a8657537df89e7a87ec3e01ca8855f04fffbb4aee31844225839c4

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