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

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for keycardai_mcp-0.3.0.tar.gz
Algorithm Hash digest
SHA256 2b40b13e3e423a7483921dc314ab4d0fbe881fb2979aac2888ed4215b7179b20
MD5 30807c2bffcefebaa85d4431f3894914
BLAKE2b-256 4d813ba4687f5410cabe9055111e01c95065e16007ba322ead17c7d012b2055a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for keycardai_mcp-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9b4593ccd6fe8d3a63f611485e1a34e3a6e504b03527c519d8165517a4aaef90
MD5 1b80bff44eef1ec8149811302773b1c8
BLAKE2b-256 ff4e2836ce6f49b208480c9e18c3b54a5991e927f8257e88ed23486ab730742c

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