Understand how MCP connects clients, servers, and LLMsThe Model Context Protocol (MCP) is built on a flexible, extensible architecture that enables seamless communication between LLM applications and integrations. This document covers the core architectural components and concepts.MCP follows a client-server architecture where:Hosts are LLM applications (like Claude Desktop or IDEs) that initiate connections
Clients maintain 1:1 connections with servers, inside the host application
Servers provide context, tools, and prompts to clients
The protocol layer handles message framing, request/response linking, and high-level communication patterns.The transport layer handles the actual communication between clients and servers. MCP supports multiple transport mechanisms:1.
Uses standard input/output for communication
Ideal for local processes
2.
Uses Server-Sent Events for server-to-client messages
HTTP POST for client-to-server messages
All transports use JSON-RPC 2.0 to exchange messages. See the specification for detailed information about the Model Context Protocol message format.MCP has these main types of messages:1.
Requests expect a response from the other side: 2.
Results are successful responses to requests: 3.
Errors indicate that a request failed: 4.
Notifications are one-way messages that don’t expect a response: 1.
Client sends initialize
request with protocol version and capabilities
2.
Server responds with its protocol version and capabilities
3.
Client sends initialized
notification as acknowledgment
4.
Normal message exchange begins
After initialization, the following patterns are supported:Request-Response: Client or server sends requests, the other responds
Notifications: Either party sends one-way messages
Either party can terminate the connection:Clean shutdown via close()
MCP defines these standard error codes:SDKs and applications can define their own error codes above -32000.Errors are propagated through:Error responses to requests
Error events on transports
Protocol-level error handlers
Here’s a basic example of implementing an MCP server:1.
Use stdio transport for local processes
Efficient for same-machine communication
Simple process management
2.
Use SSE for scenarios requiring HTTP compatibility
Consider security implications including authentication and authorization
1.
Validate inputs thoroughly
2.
Use progress tokens for long operations
Report progress incrementally
Include total progress when known
3.
Use appropriate error codes
Include helpful error messages
Clean up resources on errors
1.
Use TLS for remote connections
Validate connection origins
Implement authentication when needed
2.
Validate all incoming messages
Check message size limits
3.
Implement access controls
4.
Don’t leak sensitive information
Log security-relevant errors
3.
Test different transports
Modified at 2025-03-12 07:04:39