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-knownendpoints - 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 on0.0.0.0is rarely what you want. Bind to127.0.0.1and 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
Thestart() 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, usecreateRouter:
Best Practices
- Descriptive Names: Use clear tool/resource/prompt names
- Schema Validation: Always define proper input schemas with Zod
- Credit Pricing: Set appropriate credits based on resource cost
- Error Messages: Provide clear error messages in responses
- Graceful Shutdown: Always implement
SIGINThandlers - Wildcard URIs: Use wildcards for simpler agent registration
- Version Control: Include version in server configuration
Related Documentation
- Agents - Register your MCP agent in Nevermined
- Payment Plans - Configure pricing for your agent
- Validation of Requests - Understanding the validation flow
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)