How Does MCP Work?
MCP defines a stateful session between a client and a server, which proceeds through a formal lifecycle using messages formatted according to the JSON-RPC 2.0 specification.
1. Initialization and Capability Negotiation
This is the mandatory first phase of any connection.
The client initiates the connection by sending an initialize request. This message specifies the protocolVersion it supports and its own capabilities (e.g., support for sampling to handle LLM requests from the server).
The server must respond, indicating the protocol version it will use for the session and declaring its own
capabilities
, such as the features it offers (tools
,resources
,prompts
).
If the client and server cannot agree on a compatible protocol version, the connection is terminated. If negotiation is successful, the client sends an initialized
notification, signaling that the main operation phase can begin.

2. Operation (Message Exchange)
Once the session is active, the client and server can exchange messages based on the capabilities they agreed upon. The communication is bidirectional:
Client-to-Server:
A user's action in the host application might trigger the client to request information. For example, it can ask the server:
tools/list
to discover available toolsresources/read
to fetch the content of a specific fileprompts/list
to show the available promptstools/call
to execute a tool
Server-to-Client
A server can initiate requests to the client. The most common case is sampling/createMessage
, where a server asks the client to generate a completion from an LLM. This flow is critical for agentic behavior, but for security, the client (and by extension, the user) has final control and must approve the request before it is sent to the LLM.
3. Shutdown
The session is terminated when the client or server closes the underlying transport connection. For a local stdio server, this involves the client closing the server's input stream and terminating the process. For an HTTP transport, it means closing the HTTP connection(s).
All messages follow JSON-RPC 2.0 format with id
, method
, and params
for requests, or result/error
for responses.