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.1.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.1-py3-none-any.whl (17.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: keycardai_mcp-0.3.1.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.1.tar.gz
Algorithm Hash digest
SHA256 d99af82e0971a6742a9df43c0952e1c1bf8c6b4b21d6729046e703e32157f5a6
MD5 fe44cd3c6b2e2028e6eb4dc6f84cf82e
BLAKE2b-256 f588fcbf3e5796e885b650fe76bcd1d6e77260ed3faaa73b6ba67442264e79d0

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for keycardai_mcp-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 1797eb76082d4d3b82fadacab39b36f00dc59bfd20b3bafd3582cfa4df9f76e4
MD5 d98cac5d66cfc07263d3c877c45e00c3
BLAKE2b-256 6a7efb5e14fea86519e915e9f49c845b706c00d766458c196f54262f4b1a0e1b

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