What is MCP?
MCP (Model Context Protocol) is a protocol developed by Anthropic that lets AI assistants interact with external tools and services. When you expose your service via MCP:- AI can discover your tools: Claude can see all your methods and their parameters
- AI can call your tools: Claude can execute methods with proper inputs
- AI understands the results: Responses are formatted for AI interpretation
Quick Start
How Methods Become Tools
Each method becomes an MCP tool:| Interface Method | MCP Tool Name |
|---|---|
Create | todos.create |
List | todos.list |
Get | todos.get |
Delete | todos.delete |
resource.method.
The MCP Protocol
MCP uses JSON-RPC 2.0 as its transport. Hereβs how an AI interacts with your server:Step 1: Initialize
The AI establishes a connection:Step 2: Discover Tools
The AI asks what tools are available:Step 3: Call a Tool
The AI calls a tool:Configuration Options
Customize your MCP server:Available Options
| Option | Description |
|---|---|
WithServerInfo | Set server name and version |
WithInstructions | Provide usage guidance to the AI |
Complete Example
This example shows a complete MCP service using the recommended package-based organization:Test with curl
Using with Claude Desktop
Claude Desktop can connect to MCP servers:- Start your server:
-
Configure Claude Desktop to connect to
http://localhost:8080/mcp -
Ask Claude:
- βCreate a todo to buy groceriesβ
- βShow me all my todosβ
- βMark the first todo as completeβ
Security
Input Validation
Always validate inputs - donβt trust AI-provided data:Authentication
Add authentication with middleware:Error Handling
Method Errors
When your method returns an error, MCP wraps it withisError: true:
Protocol Errors
Invalid requests return JSON-RPC errors:Multiple Services
Each service needs its own MCP endpoint:Combining Transports
Use MCP alongside REST and JSON-RPC:Common Questions
How does the AI know what parameters to use?
Thetools/list response includes inputSchema for each tool. This JSON Schema tells the AI what parameters are expected, their types, and which are required.
Can I add descriptions to tools?
Tool descriptions come from your method organization. Use clear method names and theWithInstructions option to guide the AI.
What if the AI sends invalid data?
Your method receives the data and should validate it. Returncontract.ErrInvalidArgument for validation failures - this shows as an error to the AI.
Can I use MCP and REST together?
Yes, and itβs recommended. REST is great for testing and debugging while MCP serves AI assistants:Whatβs Next?
- REST Transport - For browser-friendly APIs
- JSON-RPC Transport - Similar protocol with batching
- Error Handling - Proper error responses
- Transports Overview - Compare all transports