Client Integration Guide
How to integrate MCP servers into your applications.
MCP clients are applications that consume MCP servers. They can be AI assistants, development tools, or any application that needs to interact with external data and tools through a standardized interface. Key concepts for MCP clients: - Connection management - Tool discovery and invocation - Error handling and retry logic - Authentication and security - Performance optimization
Before you can build MCP clients, you'll need to set up your development environment with the necessary tools and libraries.
# Install MCP Client SDK pip install mcp-client # For JavaScript/TypeScript npm install @modelcontextprotocol/client # For Python pip install mcp-sdk # Create a new client project mkdir my-mcp-client cd my-mcp-client python -m venv venv source venv/bin/activate # On Windows: venv\Scripts\activate
Learn how to establish connections with MCP servers, handle authentication, and manage server connections effectively.
import asyncio
from mcp import ClientSession, StdioClientTransport
async def connect_to_server():
# Create transport
transport = StdioClientTransport()
# Connect to server
async with ClientSession(transport) as session:
# List available tools
tools = await session.list_tools()
print(f"Available tools: {[tool.name for tool in tools.tools]}")
# Call a tool
result = await session.call_tool(
name="hello",
arguments={"name": "Alice"}
)
print(f"Result: {result.content}")
# Run the client
asyncio.run(connect_to_server())Learn how to make tool calls, handle responses, and implement error handling for robust client applications.
async def call_tools_safely(session, tool_name, arguments):
try:
result = await session.call_tool(tool_name, arguments)
return result
except Exception as e:
print(f"Error calling tool {tool_name}: {e}")
return None
async def batch_tool_calls(session, tools_to_call):
results = []
for tool_name, arguments in tools_to_call:
result = await call_tools_safely(session, tool_name, arguments)
if result:
results.append(result)
return results
# Example usage
tools_to_call = [
("get_weather", {"location": "New York"}),
("get_time", {}),
("calculate", {"expression": "2 + 2"})
]
results = await batch_tool_calls(session, tools_to_call)Create complete client applications that leverage multiple MCP servers to provide rich functionality.
class MCPClientApp:
def __init__(self):
self.sessions = {}
async def connect_to_server(self, server_name, transport):
session = ClientSession(transport)
await session.__aenter__()
self.sessions[server_name] = session
async def call_tool(self, server_name, tool_name, arguments):
if server_name not in self.sessions:
raise ValueError(f"Server {server_name} not connected")
session = self.sessions[server_name]
return await session.call_tool(tool_name, arguments)
async def close_all(self):
for session in self.sessions.values():
await session.__aexit__(None, None, None)
# Usage example
app = MCPClientApp()
await app.connect_to_server("weather", weather_transport)
await app.connect_to_server("database", db_transport)
weather = await app.call_tool("weather", "get_weather", {"location": "NYC"})
data = await app.call_tool("database", "query", {"sql": "SELECT * FROM users"})Server Development Guide
Building MCP servers
API Reference
Complete API documentation