MCP: Build Rich-Context AI Apps with Anthropic
A course by DeepLearning.AI in collaboration with Anthropic
Introduction
Welcome to the official companion repository for the course MCP: Build Rich-Context AI Apps with Anthropic, a collaborative effort between DeepLearning.AI and Anthropic. This course is designed to equip developers, engineers, and AI enthusiasts with the knowledge and practical skills to build sophisticated, context-aware AI applications using the Model Context Protocol (MCP).
In the rapidly evolving landscape of Large Language Models (LLMs), one of the most significant challenges is bridging the gap between the model's vast internal knowledge and the dynamic, specific, and proprietary data of the real world. Historically, connecting AI applications to external systems—be it databases, APIs, filesystems, or web services—has required writing bespoke, single-purpose integrations. This approach leads to a fragmented development ecosystem where every new tool or data source requires custom "glue code," resulting in duplicated effort, maintenance nightmares, and a lack of interoperability between different AI applications.
The Model Context Protocol (MCP), developed by Anthropic, is an open-standard protocol designed to solve this very problem. It standardizes how LLMs access tools, data (resources), and prompts from external sources, thereby simplifying the integration of new context into AI applications. By decoupling the AI application from the tools it uses, MCP fosters a modular and reusable ecosystem. Developers can build an "MCP server" that exposes a specific capability (e.g., interacting with a company's internal CRM), and any MCP-compatible application (an "MCP client") can instantly connect to and use that capability without any custom integration work.
This hands-on course will guide you through the core concepts of MCP's client-server architecture. You will start by understanding the "why" behind MCP, then dive deep into its components. You will take a standard chatbot, transform it into a powerful MCP-compatible application, build and deploy your own MCP servers, and learn to connect your application to a growing ecosystem of open-source servers. By the end of this course, you will be proficient in building rich-context AI applications that are modular, scalable, and ready to leverage the expanding world of MCP-compliant tools and services.
Course Topics
-
Why MCP
-
MCP Architecture
-
Chatbot Example
-
Creating an MCP Server
-
Creating an MCP Client
-
Connecting the MCP Chatbot to Reference Servers
-
Adding Prompt and Resource Features
-
Configuring Servers for Claude Desktop
-
Creating and Deploying Remote Servers
- Why MCP
The genesis of the Model Context Protocol (MCP) lies in addressing a fundamental and pervasive challenge in the development of applied AI: the integration of external context. While Large Language Models (LLMs) like Claude possess an extraordinary amount of general knowledge learned from vast training datasets, their true power is unlocked when they can interact with specific, timely, and often private information from external systems. This includes everything from searching a corporate database and calling a third-party API to reading a local file or scraping a live webpage. Without this context, an LLM is a brilliant but isolated brain. The core problem has been the how of this integration. Traditionally, developers have resorted to writing custom, hard-coded integrations for every tool and data source an AI application needs. If a chatbot needs to query a Salesforce API and a Google Calendar API, a developer would write two distinct pieces of "glue code," one for each service. This code would handle authentication, request formatting, response parsing, and error handling specific to each API.
This traditional approach suffers from several critical drawbacks that collectively lead to a fragmented and inefficient development landscape. Firstly, it results in a massive duplication of effort. A team building a customer support bot and another team building an internal sales assistant might both need to interact with Salesforce. In a pre-MCP world, both teams would likely build their own separate integrations, wasting valuable development time. Secondly, this approach creates brittle and high-maintenance systems. If Salesforce updates its API, every single application with a custom integration needs to be found and updated individually. This maintenance burden grows exponentially as the number of tools and applications increases. Thirdly, it stifles innovation and collaboration. The custom integrations are tightly coupled to the application they were built for, making them difficult to share, reuse, or open-source. A brilliant tool for interacting with the ArXiv academic database, built for one specific project, remains locked within that project's codebase, inaccessible to the wider community.
MCP was created to dismantle these silos. It introduces a standardized layer of communication, an "lingua franca," between AI applications and the tools they consume. Instead of the AI application talking directly to a dozen different APIs in a dozen different languages, it talks to all of them using one consistent protocol: MCP. The tools, in turn, are wrapped in MCP servers that translate MCP requests into actions on their specific underlying systems. This fundamental shift from a tightly-coupled, many-to-many integration mess to a decoupled, standardized client-server model offers transformative benefits. It promotes modularity and reusability; a single MCP server for Salesforce can be built once and used by any MCP-compatible application within a company. It drastically reduces maintenance overhead; if the Salesforce API changes, only the single MCP server needs to be updated, and all client applications will continue to work seamlessly. Most importantly, it fosters a collaborative ecosystem. Developers can now build and share MCP servers as standalone, reusable components. This opens the door for a public or private registry of MCP servers, allowing an AI application to dynamically discover and connect to a vast array of tools with minimal integration effort, making AI development faster, more scalable, and significantly less fragmented.
- MCP Architecture
The Model Context Protocol (MCP) is founded on a classic and robust client-server architecture. This design pattern is deliberately chosen for its simplicity, scalability, and clear separation of concerns. It decouples the AI application, which consumes context, from the external systems that provide that context. The architecture consists of two primary components: the MCP Client and the MCP Server, which communicate over a standardized protocol.
#### MCP Architecture Diagram```mermaid
graph LR
subgraph "User's Device"
User(👤 User)
App["AI Application"]
LLM[("LLM Core (e.g., Claude)")]
end
subgraph "MCP Server Ecosystem (Local or Remote)"
Server["MCP Server (e.g., ArXiv Search)"]
end
External[("🌐 External API / Service")]
User -- "1. Prompt: 'Find papers on AI'" --> App
App -- "2. MCP Client receives prompt" --> LLM
LLM -- "3. LLM decides to use a tool" --> App
App -- "4. MCP Request 📞" --> Server
Server -- "5. Executes logic (e.g., calls external API)" --> External
External -- "6. API Response" --> Server
Server -- "7. MCP Response 📦" --> App
App -- "8. Passes result to LLM" --> LLM
LLM -- "9. LLM processes result and generates answer" --> App
App -- "10. Final Answer" --> User
style User fill:#0c172b,stroke:#333,stroke-width:2px
style App fill:#31b1b5,stroke:#0f0f0f,stroke-width:2px
style LLM fill:#78401a,stroke:#00bcd4,stroke-width:2px
style Server fill:#702bba,stroke:#0f0f0f,stroke-width:2px
style External fill:#0a6141,stroke#0f0f0f,stroke-width:2px
````
Core Components:
-
MCP Client: The MCP Client is a component that resides inside the AI application. It acts as the intermediary between the LLM's reasoning engine and the network of MCP servers. Its primary responsibilities are:
- Server Discovery and Connection: The client is configured with the addresses of one or more MCP servers. Upon connection, it sends a request to each server to fetch its "manifest" or capabilities. This manifest details the tools, resources, and prompt templates the server offers, including their names, descriptions, and input/output schemas.
- Request Formulation: When the LLM decides it needs to use an external tool (a process often called "tool use" or "function calling"), the client takes the LLM's intent (e.g., "read the file 'report.md'") and formulates a structured MCP request. This request is a standardized message, likely in a format like JSON-RPC, specifying the tool to be called (
readFile) and its parameters (path: 'report.md'). - Communication: The client sends this request to the appropriate MCP server over the network (or locally via a subprocess).
- Response Handling: The client waits for the server's response, parses the standardized MCP response message, and extracts the result (e.g., the content of 'report.md'). This result is then passed back to the LLM, which uses the new information to continue its task or formulate a final answer for the user.
The key is that the client is generic; it doesn't need to know how to read a file, only how to ask an MCP server to do so according to the protocol.
-
MCP Server: The MCP Server is a standalone process that exposes a set of capabilities to any MCP client. It can be a simple script running locally on the same machine or a robust web service deployed in the cloud. Its responsibilities are the mirror image of the client's:
- Exposing Capabilities: The server's core function is to define and implement a set of tools, resources, and/or prompt templates. For example, a
filesystemserver would implement functions likereadFile,writeFile, andlistFiles. - Serving the Manifest: When a client connects, the server responds with its manifest, advertising its available capabilities. This allows the client (and by extension, the LLM) to know what it can do.
- Listening for Requests: The server listens for incoming MCP requests from clients.
- Execution and Logic: Upon receiving a valid request, the server invokes the corresponding tool's implementation. For a
readFilerequest, it would interact with the operating system's filesystem. For asearch_papersrequest on an ArXiv server, it would make an HTTP request to the actual ArXiv API. - Response Formulation: After executing the tool, the server packages the result (or any error that occurred) into a standardized MCP response message and sends it back to the client. This completes the request-response cycle.
- Exposing Capabilities: The server's core function is to define and implement a set of tools, resources, and/or prompt templates. For example, a
The communication between them is the protocol itself, defining the exact structure of request and response messages for discovering capabilities, calling tools, fetching resources, and handling errors. This rigid standardization is what enables the entire ecosystem to function seamlessly.
- Chatbot Example
To fully appreciate the transformative impact of MCP, let's consider a practical, step-by-step example: building a chatbot designed to assist researchers. The initial goal for this chatbot is to allow a user to ask questions about recent academic papers on ArXiv, a popular open-access archive for scholarly articles.
Phase 1: The Pre-MCP, Monolithic Chatbot
In a traditional development approach, we would build this functionality directly into the chatbot's codebase. The application's logic would look something like this:
- User Input: The user types a prompt: "Find me recent papers on 'causal inference'."
- LLM Invocation with Hard-coded Tools: The chatbot's backend code would be designed to recognize the intent to search for papers. It would have a specific Python function, say
search_arxiv_directly(query: str), defined within its own source code. The LLM would be prompted in a way that encourages it to "call" this specific, known function. - Custom Integration Logic: The
search_arxiv_directlyfunction would contain all the necessary logic to interact with the ArXiv API. This includes:- Constructing the correct HTTP request URL with the user's query.
- Making the HTTP GET request using a library like
requests. - Handling potential network errors (timeouts, connection issues).
- Parsing the XML or JSON response from the ArXiv API.
- Extracting the relevant information (titles, authors, summaries) for each paper.
- Formatting this data into a clean string or data structure.
- Returning Context to LLM: The formatted list of papers is then passed back into a subsequent prompt for the LLM.
- Final Response Generation: The LLM receives the search results and synthesizes a natural language response for the user, such as: "I found three recent papers on causal inference. The first is titled 'A New Approach to...' by..."
This chatbot works, but it's brittle and inflexible. If we now want to add a new feature to search for papers on PubMed, we have to go back into the chatbot's core code, write a new function search_pubmed_directly, and repeat the entire process of writing custom integration logic. The chatbot's codebase becomes increasingly bloated and tightly coupled to the specific tools it uses.
Phase 2: Transforming the Chatbot into an MCP-Compatible Application
Now, we refactor our application to use MCP. The goal is to decouple the chatbot's core logic from the tool's implementation.
- Create an MCP Server: First, we take the logic from our
search_arxiv_directlyfunction and move it into a completely separate application: an ArXiv MCP Server. This server's sole job is to expose one tool:search_academic_papers(query: str). We use a framework likeFastMCPto easily wrap our existing logic, which handles all the protocol-level communication for us. We run this server (it could be locally or remotely). - Modify the Chatbot to be an MCP Client: Next, we modify the chatbot itself. We completely remove the
search_arxiv_directlyfunction and its custom integration code. In its place, we add a generic MCP Client. - The New Workflow: The user interaction now looks different under the hood:
- User Input: "Find me recent papers on 'causal inference'."
- MCP Discovery: At startup (or dynamically), our chatbot's MCP Client connects to the ArXiv MCP Server's address. It asks the server for its capabilities and learns that a tool named
search_academic_papersexists, which takes aquerystring as input and is described as "Searches the ArXiv database for academic papers." - LLM Invocation with Discovered Tools: The chatbot now presents this dynamically discovered tool information to the LLM. The LLM's reasoning engine sees the user's request and determines that the
search_academic_paperstool is the perfect fit. It decides to call this tool with the parameterquery='causal inference'. - Client-Server Communication: The MCP Client takes this instruction, creates a standardized MCP request, and sends it to the ArXiv MCP Server.
- Server Execution: The ArXiv MCP Server receives the request, calls its internal implementation (the logic we moved), interacts with the ArXiv API, and sends the formatted results back in a standardized MCP response.
- Context and Response: The MCP Client passes the results back to the LLM, which then generates the final answer for the user, just as before.
The magic here is that the chatbot's code no longer contains any specific knowledge about ArXiv. It only knows how to speak MCP. If we now want to add PubMed search, we don't touch the chatbot at all. We simply create a new PubMed MCP Server and tell our chatbot's client its address. The chatbot will automatically discover the new search tool and make it available to the LLM, demonstrating the power of modularity and dynamic tool integration.
- Creating an MCP Server
Creating an MCP server is the process of taking a set of functionalities—whether it's interacting with an API, a database, or a local filesystem—and exposing them to the world through the standardized Model Context Protocol. This is a pivotal skill in the MCP ecosystem, as servers are the fundamental building blocks that provide context to AI applications. This course module focuses on the practical steps of building, testing, and running your own server, using specialized frameworks like FastMCP to abstract away the low-level complexities of the protocol itself. The process can be broken down into a clear, logical sequence of steps.
Step 1: Defining the Capabilities (The "What")
Before writing any code, the first step is to conceptualize what your server will do. You need to define the specific tools, resources, and prompt templates it will offer. For our ArXiv chatbot example, the primary capability is a tool. We would define it as follows:
- Tool Name:
search_academic_papers - Description: "A tool to search the ArXiv.org database for academic papers based on a query string. Returns a list of recent papers with their titles, authors, and summaries." This description is crucial, as it's what the LLM will use to understand when and how to use the tool.
- Input Schema: The tool requires one argument,
query, which is of typestring. - Output Schema: The tool will return a list of objects, where each object contains
title(string),authors(list of strings), andsummary(string).
Defining this "interface" first is a critical design step. It forces you to think clearly about the server's purpose and how it will be used by a client.
Step 2: Implementing the Core Logic (The "How")
This is where you write the actual code that performs the work. This is the logic that we previously had embedded inside our monolithic chatbot. You would write a Python function, for instance, def execute_arxiv_search(query: str) -> list:. Inside this function, you would place all the code responsible for:
- Sanitizing the input
query. - Constructing the appropriate URL for the ArXiv API.
- Using an HTTP client library (
requests,httpx) to make the API call. - Implementing robust error handling (e.g., what happens if ArXiv is down? What if the query returns no results?).
- Parsing the complex response from the API (which might be in XML or another format).
- Transforming the parsed data into the clean, simple output structure you defined in Step 1.
This core logic is pure functionality; at this stage, it has no knowledge of MCP. It's a self-contained, testable piece of code.
Step 3: Wrapping the Logic with an MCP Framework (The "Serverization")
This is where a framework like FastMCP comes into play. Instead of manually implementing a web server, parsing raw MCP requests, and formatting MCP responses (which would be tedious and error-prone), you use the framework to do the heavy lifting. The process typically looks like this:
- Instantiate the Server: You create an instance of the
FastMCPServer. - Register the Tool: You "register" your core logic function with the server instance. This involves telling the framework about your tool—its name, description, schemas, and which function to call when the tool is invoked. A framework might use decorators or a registration function, like
server.add_tool(name="search_academic_papers", description=..., func=execute_arxiv_search). - Run the Server: You start the server with a single command, like
server.run(host="0.0.0.0", port=8000).
TheFastMCPframework automatically handles the rest. It will start a web server, create an endpoint that listens for MCP requests, serve the manifest (based on the tools you registered), parse incoming tool call requests, invoke yourexecute_arxiv_searchfunction with the correct parameters, take the return value, and wrap it in a valid MCP response to send back to the client.
Step 4: Testing the Server (The "Verification")
Once your server is running, you need to verify that it's behaving correctly. This is where a tool like the MCP Inspector becomes invaluable. The MCP Inspector is a specialized MCP client designed for debugging. You would point the Inspector at your server's address (e.g., http://localhost:8000). It will first attempt to fetch the server's manifest, allowing you to see if your tool is advertised correctly. Then, you can use the Inspector's UI to manually invoke your search_academic_papers tool, providing a test query. You can inspect the raw MCP request being sent and, most importantly, the raw MCP response coming back, ensuring that your server is fully compliant with the protocol before you even connect it to a real AI application. This iterative cycle of building, running, and testing is key to developing reliable MCP servers.
- Creating an MCP Client
If an MCP server is the provider of context, the MCP client is the consumer. Creating an MCP client involves integrating a specialized component into your AI application (like our researcher chatbot) that enables it to communicate with any MCP-compliant server. The beauty of this architecture is that the client code you write is generic. It doesn't need to know the specifics of any particular tool, such as how to search ArXiv or how to read a file. It only needs to understand the language of MCP—how to discover capabilities, how to request a tool execution, and how to interpret the response. This module of the course focuses on building this client-side logic, which effectively turns your application into a gateway to the entire MCP ecosystem.
The responsibilities of an MCP client can be structured into a distinct lifecycle:
1. Initialization and Configuration:
The first step is to initialize the client within your application. This typically involves creating an instance of a client class from an MCP library. The primary configuration required is a list of server addresses. This list tells the client where to look for tools and resources.```python
Pseudocode for client initialization
mcp_server_urls = [
"http://localhost:8000", # Our local ArXiv server
"http://mcp.anthropic.com/filesystem", # A public reference server
]
mcp_client = MCPClient(servers=mcp_server_urls)
This configuration can be static, loaded from a file, or even discovered dynamically, but the core idea is to provide the client with entry points into the MCP network.
**2. Capability Discovery and Manifest Aggregation:**
Once initialized, the client's first action is to connect to each configured server and request its manifest. The manifest is a JSON object detailing all the tools, resources, and prompts offered by that server. The client's job is to:
* Iterate through each server URL.
* Send a standardized MCP request to each one (e.g., a `GET /mcp` request or a specific RPC call like `mcp_getServerInfo`).
* Receive and parse the manifest from each server.
* Aggregate all these capabilities into a single, unified list. This aggregated list represents the *total* set of external tools available to the AI application.
This discovery process is vital because it's what makes the system dynamic. When you add a new server to the configuration list, its tools automatically appear in this aggregated list without any other code changes. The client creates a comprehensive "menu" of available actions for the LLM.
**3. Presenting Tools to the LLM:**
The LLM cannot use tools it doesn't know about. The client's next critical role is to format the aggregated list of tools into a structure that the LLM can understand. This is typically done by injecting the tool descriptions into the system prompt. For each tool discovered, the client will format a description block, including its name, a natural language description of what it does, and its input parameters. For example:```xml
<tools>
<tool>
<name>search_academic_papers</name>
<description>Searches the ArXiv.org database for academic papers...</description>
<parameters>
<param name="query" type="string" />
</parameters>
</tool>
<tool>
<name>readFile</name>
<description>Reads the content of a file from the local filesystem...</description>
<parameters>
<param name="path" type="string" />
</parameters>
</tool>
</tools>
When the user interacts with the chatbot, this block of XML (or a similar format) is included in the prompt, effectively telling the LLM, "Here are the external capabilities you have at your disposal."
4. Handling Tool Use Requests from the LLM:
Modern LLMs like Claude can be prompted to indicate when they want to use a tool. When the LLM decides to make a tool call, its response will contain a special structure indicating which tool to use and with what arguments. For instance, the LLM's output might contain:xml
<invoke_tool>
<name>search_academic_papers</name>
<parameters>
<query>causal inference</query>
</parameters>
</invoke_tool>
The application code, which is monitoring the LLM's output, will detect this structure. It then passes this information to the MCP client.
5. Executing the Tool Call via MCP:
This is the core communication step. The MCP client receives the tool invocation request from the application. It then:
- Identifies which server provides the requested tool (
search_academic_papers). It knows this from the discovery phase. - Constructs a formal MCP tool-call request message (e.g., a JSON-RPC object).
- Sends this request to the correct server's URL.
- Awaits the server's response.
- Parses the MCP response to extract the result of the tool execution.
-
Returns this result to the main application logic. The application then feeds this result back into the LLM for the final step, completing the loop. This cycle of discovery, presentation, invocation, and execution is the essence of a functioning MCP client.
-
Connecting the MCP Chatbot to Reference Servers
This is the module where the true power and elegance of the Model Context Protocol become strikingly apparent. After successfully building a custom MCP server (for ArXiv search) and integrating a generic MCP client into our chatbot, we can now demonstrate the protocol's primary benefit: effortless extensibility. Connecting our chatbot to new, pre-existing servers requires no changes to the chatbot's core logic or its custom ArXiv server. The only change required is to update the list of server URLs in the MCP client's configuration. This showcases the remarkable decoupling and interoperability that MCP enables. The course uses reference servers built by Anthropic's MCP team to illustrate this concept, with two prominent examples being the filesystem server and the fetch server.
1. The Filesystem Server
The filesystem server is an MCP server designed to expose basic file system operations as tools. When our chatbot's MCP client is configured with the address of this server, it will automatically discover a new set of tools during its initialization phase. These tools might include:
readFile(path: string): Reads the entire content of a file at a given path.writeFile(path: string, content: string): Writes or overwrites a file with the provided content.listFiles(path: string): Lists all files and directories within a given path.createDirectory(path: string): Creates a new directory.
Impact on the Chatbot:
Suddenly, our researcher chatbot, which was previously only capable of searching the web for academic papers, gains a whole new dimension of interactivity. The LLM, being aware of these newly discovered tools via the system prompt, can now fulfill a much wider range of user requests. The user can now have conversations like:
- User: "Find me papers on 'transformer architectures', and save the results to a file named
transformers_reading_list.md." - Chatbot's Internal Process:
- LLM sees the request and decides to first use the
search_academic_paperstool. - The MCP client calls our ArXiv server, gets the results.
- The results are fed back to the LLM. The LLM now sees the second part of the request.
- LLM formats the paper list into Markdown and decides to use the
writeFiletool. - The MCP client calls the
filesystemserver withpath='transformers_reading_list.md'and the formatted content. - The file is created on the local disk.
- The LLM confirms to the user: "I have found the papers and saved the list to
transformers_reading_list.md."
- LLM sees the request and decides to first use the
Without writing a single new line of integration code in the chatbot, we have seamlessly extended its capabilities.
2. The Fetch Server
The fetch server is another powerful reference tool. Its purpose is to retrieve content from the web. It might expose a single, versatile tool:
fetch(url: string): Takes a URL, fetches the content of the web page, and intelligently extracts the main body of text, often converting it to clean Markdown and stripping away ads, navigation bars, and other boilerplate.
Impact on the Chatbot:
By simply adding the fetch server's address to our client's configuration, our chatbot's abilities expand yet again. It can now act as a research assistant that can not only find papers but also read and summarize web articles.
- User: "Here is a link to a blog post about a new AI model:
[some-url]. Can you summarize its key points and then find related academic papers?" - Chatbot's Internal Process:
…(正文过长,页面只展示前面部分,完整内容见仓库)
