By Abhijith N M · Reviewed 24 October 2026 · Editorial standards
You've built an MCP server that exposes useful tools. Now you need to connect it to an AI agent so the model can actually call those tools. The client side of this connection is where most integration problems happen—timeouts, forgotten namespacing, context bloat, and state management issues all live here. Understanding what the client does and what it's responsible for will save you hours of debugging.
An MCP client in an AI application runs a simple repeating cycle. It starts by connecting to the server—either spawning a local process or opening an HTTP connection. Once connected, it discovers what tools the server offers and retrieves their full definitions.
The client then hands those tool definitions to the model. When the model decides it needs to call a tool, the client makes that call and streams the result back into the model's context. This loop continues until the model produces a final answer.
Most AI apps use a config file to wire up their MCP servers. The file format varies by framework, but the structure is consistent: a list of server entries, each with either a command to spawn locally or a URL for remote access.
{
"mcpServers": {
"notes": {
"command": "python",
"args": ["/path/to/server.py"]
},
"company-crm": {
"url": "https://mcp.example.com/mcp"
}
}
}You almost never write MCP protocol code from scratch. Agent frameworks like LangGraph, Anthropic's SDK, and others ship built-in MCP client adapters. These handle connection management, tool listing, and result handling for you.
The pattern is always the same: initialize a connection with server parameters, create a client session, list the available tools, and call them when the model requests it. The details vary slightly per SDK, so check your framework's README for the exact API.
# Shape of the official Python SDK client - check the README for your SDK version
from mcp import ClientSession, StdioServerParameters
from mcp.client.stdio import stdio_client
params = StdioServerParameters(command="python", args=["server.py"])
async def main():
async with stdio_client(params) as (read, write):
async with ClientSession(read, write) as session:
tools = await session.list_tools()
print([t.name for t in tools.tools])
result = await session.call_tool("add_note", {"text": "hello"})
print(result.content)
The framework does the plumbing, but you're still responsible for several things that are easy to overlook.
Connecting ten servers with 200 total tools creates a bloated prompt. The model sees too many options and gets confused about which tool to use. You have three strategies.
First, connect only the servers the current task needs. Second, filter tools per agent—different agents get different subsets. Third, use a gateway with tool search so the model queries for relevant tools instead of seeing the full list upfront.
Written and edited by the nexGenCoders team. Spotted a mistake? Tell us and we will fix it.
Don't lose this lesson
Create a free account to save your progress, keep your streak, and work toward a shareable certificate.