All posts
Agentic AI9 min read·

Building MCP Servers: How to Give Your AI Agents Real-World Tools

A practical guide to building Model Context Protocol (MCP) servers that connect LangChain agents to live data, APIs, and local tools - the architecture powering the next generation of agentic AI.

MF

Muhammad Farhan

AI Engineer · Founder of Datraxa

Model Context Protocol (MCP) is the fastest-moving standard in agentic AI right now. Introduced by Anthropic and quickly adopted across the ecosystem, MCP gives AI agents a standardized way to call tools, read files, query databases, and interact with external APIs - without custom glue code for every integration.

What MCP Solves

Before MCP, every agent framework had its own tool interface. LangChain tools, OpenAI function calls, Anthropic tool use - they all worked differently. If you built a database query tool for LangChain, it did not work with Claude directly. You rewrote it for every integration.

MCP standardizes this. An MCP server exposes tools, resources, and prompts over a well-defined protocol (JSON-RPC 2.0 over stdio or HTTP/SSE). Any MCP-compatible client - Claude Desktop, LangChain, Cursor, your own agent - can connect and use the same server without modification. Build once, run anywhere.

MCP Architecture: Three Primitives

  • -Tools - Functions the agent can call. Takes structured input, returns structured output. Think database queries, API calls, file operations.
  • -Resources - Read-only data sources the agent can access. Files, database rows, API responses served as readable URIs.
  • -Prompts - Reusable prompt templates the agent can invoke by name. Useful for standardizing common reasoning patterns.

For most production use cases, tools are what you actually need. Resources and prompts are useful extras once your core tool layer works.

Building an MCP Server in Python

The official Python SDK makes this straightforward. You install it with pip, create a Server instance, then decorate async functions with @app.list_tools() to declare what tools exist and @app.call_tool() to handle calls. Each tool declares a name, a natural-language description the model reads to decide when to use it, and a JSON schema for its input parameters. The server runs over stdio by default, meaning your agent spawns it as a subprocess and communicates through stdin/stdout.

The most important thing to get right is the tool description. The model reads this text at inference time to decide whether to call your tool. Write it from the model perspective: what situation triggers this tool, what inputs it expects, what it returns. A vague description produces random tool usage. A precise description produces reliable tool usage.

Connecting to LangChain

LangChain supports MCP natively via the langchain-mcp-adapters package. You point a MultiServerMCPClient at your server process (specifying the command to launch it and the transport type), call get_tools() to retrieve the tool list as standard LangChain tools, then pass them to any LangChain agent or LangGraph ReAct agent exactly as you would hand-written tools. The adapter handles serialization, error propagation, and the stdio lifecycle automatically.

Production Patterns Worth Knowing

ConcernLocalRemoteRecommendation
TransportstdioHTTP/SSEstdio is simpler; HTTP/SSE enables remote servers and multi-client
AuthNone (local)OAuth 2.1 / API keysRemote MCP servers require auth; local stdio servers trust the host process
Error handlingRaise ValueErrorReturn error TextContentReturning errors as content keeps the agent loop running
Tool granularityOne tool per actionBatch operationsSmaller tools give agents more precise control

What Makes a Good MCP Tool

  • -Name it like a function, not a feature. search_recent_news beats news_tool. The model uses the name to decide when to call it.
  • -Write the description for the model, not the developer. Explain when to use it and what it returns, not how it works internally.
  • -Return structured text, not raw JSON. Agents parse text more reliably than embedded JSON strings in most current models.
  • -Keep tool scope narrow. A tool that does one thing well beats a tool with five optional parameters and complex branching logic.

Frequently Asked Questions

Is MCP only for Claude?

No. MCP is an open standard and client implementations exist for LangChain, LlamaIndex, OpenAI Agents SDK, and others. Claude Desktop was the first major consumer, but the protocol is model-agnostic.

How is MCP different from OpenAI function calling?

OpenAI function calling is model-specific and baked into the API request/response cycle. MCP is a separate server protocol that runs independently of any specific model or API. Your MCP server works with any MCP client - swap the underlying model without touching the tool layer.

Should I use MCP for every agent project?

Not necessarily. For a single-model, single-purpose agent with two or three tools, standard tool calling is simpler and has less overhead. MCP earns its complexity when you need the same tools accessible across multiple agents, models, or environments - or when you want to share tools with third-party MCP clients like Claude Desktop or Cursor.

Share this article

Work with me

Need This Built?

I am available for freelance projects, consulting, and remote AI engineering roles. If you need an agentic system, CV pipeline, or scraping infrastructure built properly - let's talk.

Usually responds within 24 hours  ·  Based in Islamabad, open to remote globally