Skip to main content

MCP Integration

The Model Context Protocol (MCP) integration provides a complete payment-protected MCP server with OAuth 2.1 support and automatic credit management. This is the simplest way to run a payment-protected AI agent.

Overview of MCP Integration

The MCP integration automatically handles:
  • OAuth 2.1 Discovery: Auto-configured .well-known endpoints
  • Paywall Protection: Automatic token verification and credit burning
  • Streaming Support: SSE (Server-Sent Events) for streaming responses
  • Credit Management: Fixed or dynamic credit costs per tool/resource/prompt
  • Client Registration: Dynamic client registration for OAuth flows

OAuth 2.1 Discovery

When you start an MCP server, the library automatically exposes OAuth 2.1 discovery endpoints required by the X402 spec: These endpoints are generated automatically—no manual configuration required.

Configure MCP

Initialize the MCP integration with your agent details:

Register Tools with Credits

Register MCP tools and specify their credit cost:

Dynamic Credit Calculation

For variable costs based on input/output:

Register Resources

Register MCP resources with payment protection:

Register Prompts

Register MCP prompts with credit costs:

Start Managed Server

Start a complete MCP server with all endpoints configured:
🔐 Bind to localhost in development. The MCP server holds an OAuth issuer and accepts paid requests — exposing it directly on 0.0.0.0 is rarely what you want. Bind to 127.0.0.1 and front it with a reverse proxy (Caddy, nginx, Traefik) terminating TLS for any public traffic.

MCP Logical URIs

When registering your agent at nevermined.app, use MCP logical URIs instead of HTTP URLs:

URI Format

Examples

Wildcard Registration

Use wildcards to register all tools/resources/prompts at once:

Auto-Configured Endpoints

The start() method automatically creates these endpoints:

Complete Example: Weather Agent

Handler Options

Response Metadata (_meta)

After each paywall-protected call, the SDK injects a _meta field into the response following the MCP specification. This field is always present regardless of whether credit redemption succeeded or failed:

Error Handling

The paywall automatically returns 402 errors for invalid/missing tokens:

Advanced: Custom Express App

For existing Express applications, use createRouter:

Best Practices

  1. Descriptive Names: Use clear tool/resource/prompt names
  2. Schema Validation: Always define proper input schemas with Zod
  3. Credit Pricing: Set appropriate credits based on resource cost
  4. Error Messages: Provide clear error messages in responses
  5. Graceful Shutdown: Always implement SIGINT handlers
  6. Wildcard URIs: Use wildcards for simpler agent registration
  7. Version Control: Include version in server configuration

Source References:
  • RUN.md (MCP Server section, lines 16-86)
  • src/mcp/index.ts (MCP integration API)
  • tests/integration/mcp-integration.test.ts (integration examples)