Model Context Protocol (MCP): End‑to‑End Tutorial with a Hands‑On News Retrieval Project
This tutorial explains the Model Context Protocol (MCP) for integrating large language models with external data sources, details its architecture, connection lifecycle, and core JSON‑RPC features, and provides a step‑by‑step Python implementation that fetches real‑time tech news and integrates with Claude Desktop.
Model Context Protocol (MCP) is an open protocol developed by Anthropic that standardizes communication between large language models (LLMs) and external data sources or tools, similar to a USB‑C interface for AI.
The article first explains the concept of a protocol, then introduces MCP, its architecture (host program, client, server), connection lifecycle (initialization, message exchange, termination), and core features such as resources, prompts, and tools, which are implemented using JSON‑RPC 2.0.
A hands‑on project is presented: a Python server that fetches real‑time tech news. The code uses uv for package management, httpx for async HTTP requests, BeautifulSoup for HTML parsing, and the FastMCP class to expose a tool get_tech_news to the LLM.
# MacOS/Linux安装命令
curl -Ls https://astral.sh/uv/install.sh | sh
# Windows安装命令
irm https://astral.sh/uv/install.ps1 | iex uv init mcp-server-project
cd mcp-server-project # create virtual env
uv venv
# activate for macos/linux
source .venv/bin/activate
# activate for windows
.venv\Scripts\activate
# install libraries
uv add "mcp[cli]" httpx bs4 dotenv from mcp.server.fastmcp import FastMCP
from dotenv import load_dotenv
import httpx
import os
from bs4 import BeautifulSoup
import json
load_dotenv()
# initialize server
mcp = FastMCP("tech_news")
USER_AGENT = "news-app/1.0"
NEWS_SITES = {"arstechnica": "https://arstechnica.com"}
async def fetch_news(url: str):
"""It pulls and summarizes the latest news from the specified news site."""
async with httpx.AsyncClient() as client:
try:
response = await client.get(url, timeout=30.0)
soup = BeautifulSoup(response.text, "html.parser")
paragraphs = soup.find_all("p")
text = " ".join([p.get_text() for p in paragraphs[:5]])
return text
except httpx.TimeoutException:
return "Timeout error"
@mcp.tool()
async def get_tech_news(source: str):
"""
Fetches the latest news from a specific tech news source.
Args:
source: Name of the news source (for example, "arstechnica" or "techcrunch").
Returns:
A brief summary of the latest news.
"""
if source not in NEWS_SITES:
raise ValueError(f"Source {source} is not supported.")
news_text = await fetch_news(NEWS_SITES[source])
return news_text
if __name__ == "__main__":
mcp.run(transport="stdio")Setup steps include installing uv , initializing a project with uv init , creating a virtual environment, installing dependencies, editing main.py , and configuring Claude Desktop via a JSON file that points to the server executable.
The article then shows how to test the integration in Claude Desktop, troubleshoot errors by checking logs, and suggests extensions such as multi‑source aggregation, OAuth2 authentication, and Redis caching for performance.
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.