Introducing Model Context Protocol (MCP): An Open Standard for LLM Integration with Data Sources and Tools
The article explains Anthropic's open Model Context Protocol (MCP), detailing its client‑server architecture, resource and prompt definitions, tool discovery and execution, sampling workflow, security features, and provides a complete Python example that demonstrates building, running, and testing an MCP server and client for real‑time data retrieval.
MCP (Model Context Protocol) is an open protocol released by Anthropic that enables large language models (LLMs) to seamlessly integrate with external data sources and tools, positioning itself as the "HTTP of AI" to promote standardisation and decentralisation of LLM applications.
Basic concepts : MCP defines a client‑server architecture where the host (the LLM application) initiates a 1:1 connection to a server that supplies context, tools, and prompts. The server controls resources, avoiding the need to expose API keys to the host, thereby enhancing security.
Resources : Any data the server wishes to provide—files, database records, API responses, images, logs, etc.—is identified by a unique URI and described with a simple JSON schema:
{
uri: string; // Unique identifier for the resource
name: string; // Human‑readable name
description?: string; // Optional description
mimeType?: string; // Optional MIME type
}Prompts : Pre‑defined templates that can accept dynamic arguments, context, and can be presented as UI elements (e.g., slash commands). Prompt schema example:
{
name: string; // Unique identifier for the prompt
description?: string; // Human‑readable description
arguments?: [
{
name: string; // Argument identifier
description?: string;
required?: boolean;
}
];
}Tools : Executable functions exposed by the server. Clients discover tools via tools/list and invoke them with tools/call . Each tool is described by a JSON schema:
{
name: string; // Unique identifier for the tool
description?: string; // Human‑readable description
inputSchema: {
type: "object",
properties: { ... } // Tool‑specific parameters
}
}Sampling : MCP supports a sampling workflow where the server requests a message from the client, the client reviews/modifies it, the LLM generates a response, and the result is returned to the client for final handling. The message format follows a structured JSON schema.
Advantages :
Standardisation: Provides a uniform way to connect LLMs to diverse data sources, similar to USB‑C for hardware.
Flexibility: Allows easy switching between models and tools without redeveloping adapters.
Openness: Anyone can implement an MCP server, fostering an ecosystem comparable to HTTP/REST.
Security: Strict permission controls keep data owners in control of access.
Example implementation : The article walks through a complete Python example using the FastMCP class.
Server setup:
from typing import Any
from mcp.server.fastmcp import FastMCP
# Initialise FastMCP server
mcp = FastMCP("Path of Exile 2 hotfix")
target_url = "https://www.pathofexile.com/forum/view-forum/2212"Helper function (simple async web‑scraper):
async def poe2_hotfix(url: str):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'
}
async with httpx.AsyncClient() as client:
try:
response = await client.get(url, headers=headers, timeout=30.0)
soup = BeautifulSoup(response.text, 'html.parser')
table = soup.find('table')
result_text = ""
if table:
for row in table.find_all('tr'):
cells = row.find_all('td')
if cells:
for cell in cells:
result_text += cell.get_text(strip=True) + '\n'
result_text += '-' * 50 + '\n'
else:
print('未找到表格元素')
return result_text
except Exception:
return NoneTool definition:
@mcp.tool()
async def find_poe2_hotfix() -> str:
hotfix_data = await poe2_hotfix(target_url)
if not hotfix_data:
return "Unable to find any hotfix in office"
return hotfix_dataRunning the server:
if __name__ == "__main__":
# Initialise and run the server
mcp.run(transport='stdio')Client side testing can be performed with the MCP Inspector:
pip install mcp
mcp dev server.pyAfter registering the tool, the LLM can answer queries such as "What is the latest Path of Exile 2 hotfix?" by invoking the tool, retrieving the scraped data, and responding with the version and release time. Queries unrelated to the tool are rejected, demonstrating safe tool usage.
The article concludes with suggestions for extending prompts, adding more sophisticated crawlers, and integrating the protocol into larger AI agents.
DevOps
Share premium content and events on trends, applications, and practices in development efficiency, AI and related technologies. The IDCF International DevOps Coach Federation trains end‑to‑end development‑efficiency talent, linking high‑performance organizations and individuals to achieve excellence.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.