Skip to main content

Authed authentication integration for Model Context Protocol (MCP)

Project description

Authed MCP Integration

This package provides integration between Authed authentication and the Model Context Protocol (MCP).

Overview

The Authed MCP integration allows you to:

  1. Create MCP servers with Authed authentication
  2. Create MCP clients that can authenticate with Authed
  3. Register MCP servers as Authed agents
  4. Grant access permissions between MCP clients and servers

Installation

pip install authed-mcp

Usage

Server Example

import asyncio
import os
from dotenv import load_dotenv

from authed import Authed
from authed_mcp import AuthedMCPServer

async def main():
    # Load environment variables
    load_dotenv()
    
    # Initialize Authed client
    registry_url = os.getenv("AUTHED_REGISTRY_URL", "https://api.getauthed.dev")
    agent_id = os.getenv("AUTHED_AGENT_ID")
    agent_secret = os.getenv("AUTHED_AGENT_SECRET")
    private_key = os.getenv("AUTHED_PRIVATE_KEY")
    public_key = os.getenv("AUTHED_PUBLIC_KEY")
    
    # Create MCP server with Authed authentication
    server = AuthedMCPServer(
        name="example-server",
        registry_url=registry_url,
        agent_id=agent_id,
        agent_secret=agent_secret,
        private_key=private_key,
        public_key=public_key
    )
    
    # Register a resource handler
    @server.resource("/hello/{name}")
    async def hello_resource(name: str):
        return f"Hello, {name}!", "text/plain"
    
    # Register a tool handler
    @server.tool("echo")
    async def echo_tool(message: str):
        return {"message": message}
    
    # Register a prompt handler
    @server.prompt("greeting")
    async def greeting_prompt(name: str = "World"):
        return f"Hello, {name}! Welcome to the MCP server."
    
    # Run the server
    server.run()

if __name__ == "__main__":
    asyncio.run(main())

Client Example

import asyncio
import os
from dotenv import load_dotenv

from authed_mcp import AuthedMCPClient

async def main():
    # Load environment variables
    load_dotenv()
    
    # Initialize client with Authed credentials
    registry_url = os.getenv("AUTHED_REGISTRY_URL", "https://api.getauthed.dev")
    agent_id = os.getenv("AUTHED_AGENT_ID")
    agent_secret = os.getenv("AUTHED_AGENT_SECRET")
    private_key = os.getenv("AUTHED_PRIVATE_KEY")
    public_key = os.getenv("AUTHED_PUBLIC_KEY")
    
    # Create MCP client with Authed authentication
    client = AuthedMCPClient(
        registry_url=registry_url,
        agent_id=agent_id,
        agent_secret=agent_secret,
        private_key=private_key,
        public_key=public_key
    )
    
    # Get server agent ID
    server_agent_id = os.getenv("MCP_SERVER_AGENT_ID")
    
    # Define server URL
    server_url = "http://localhost:8000/sse"
    
    try:
        # Call a tool
        result = await client.call_tool(
            server_url=server_url,
            server_agent_id=server_agent_id,
            tool_name="echo",
            arguments={"message": "Hello from MCP client!"}
        )
        print(f"Echo result: {result}")
        
        # List available resources
        resources = await client.list_resources(
            server_url=server_url,
            server_agent_id=server_agent_id
        )
        print(f"Available resources: {resources}")
    except Exception as e:
        print(f"Error: {str(e)}")

if __name__ == "__main__":
    asyncio.run(main())

Components

AuthedMCPServer

A wrapper around the MCP server that adds Authed authentication. It initializes both the Authed client and the MCP server, and registers the necessary middleware to handle authentication.

AuthedMCPClient

A client for making authenticated requests to MCP servers. It automatically:

  • Creates and attaches DPoP proofs to requests
  • Handles token management
  • Provides a simple interface for interacting with MCP servers
  • Includes improved URL normalization for consistent comparison
  • Provides robust error handling

Utility Functions

register_mcp_server

Registers an MCP server as an agent in Authed:

from authed import Authed
from authed_mcp import register_mcp_server

async def setup():
    authed = Authed.initialize(...)
    
    server_info = await register_mcp_server(
        authed=authed,
        name="My MCP Server",
        description="MCP server with Authed authentication",
        capabilities={"tools": ["echo", "calculator"], "resources": ["hello"]}
    )
    
    # Save the resulting agent_id, private_key, etc.
    print(f"Server registered with ID: {server_info['agent_id']}")

grant_mcp_access

Grants an MCP client access to an MCP server:

from authed import Authed
from authed_mcp import grant_mcp_access

async def setup_permissions():
    authed = Authed.initialize(...)
    
    success = await grant_mcp_access(
        authed=authed,
        client_agent_id="client-agent-id",
        server_agent_id="server-agent-id",
        permissions=["mcp:call_tool", "mcp:list_resources"]
    )
    
    if success:
        print("Access granted successfully")
    else:
        print("Failed to grant access")

Requirements

  • Python 3.8+
  • Authed SDK
  • MCP SDK

License

MIT

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

authed_mcp-0.1.3.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

authed_mcp-0.1.3-py3-none-any.whl (12.1 kB view details)

Uploaded Python 3

File details

Details for the file authed_mcp-0.1.3.tar.gz.

File metadata

  • Download URL: authed_mcp-0.1.3.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.6

File hashes

Hashes for authed_mcp-0.1.3.tar.gz
Algorithm Hash digest
SHA256 1ec890f9f1134a9605360115102cd877af3b533db5378ab119da417d4c44b742
MD5 17373e13b4552f066cb9bcc66721ff30
BLAKE2b-256 fa8d8b7a3b67a8bad8ad4b6fd5c90d68c42e9785fecf13b7e784ff0c4c8af564

See more details on using hashes here.

File details

Details for the file authed_mcp-0.1.3-py3-none-any.whl.

File metadata

  • Download URL: authed_mcp-0.1.3-py3-none-any.whl
  • Upload date:
  • Size: 12.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.6

File hashes

Hashes for authed_mcp-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 0252004c710ba27de8335c1efceae77431fc70c47e8af1ec5c58b549aef799e8
MD5 cc0a83841a6766012101a7011e00ef55
BLAKE2b-256 9a7ae636d2fbbd2430df7e44becf16ad3901d10df51bd7c1052ecfdf0e35bfe5

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