Model Context protocol (mcp)

MCP (Model Context Protocol) is a standardized way to connect AI systems to external tools and services. Instead of building custom integrations for each tool, MCP provides a unified interface, like USB for AI tools.

MCP architecture

The MCP client connects to servers that expose tools through a standard protocol.

Tool calling defines tools in your code. MCP standardizes how agents discover and connect to external tool servers. Think of tool calling as writing your own functions, while MCP is like plugging into a shared ecosystem of pre-built tools that any agent can use.

patterns/10_mcp.py
python
class MCPAgent:
    def __init__(self, server_type="filesystem"):
        self.llm = get_llm()
        self.mcp_client = MCPClient(server_type=server_type)
        self.available_tools = self.mcp_client.list_tools()

    def process_query(self, query):
        # Show available tools to the LLM
        tools_desc = self.get_tools_description()
        prompt = f"""
        Available tools: {tools_desc}
        Query: {query}
        Which tool should I use? Format: TOOL: name, INPUT: data
        """
        response = self.llm.generate(prompt).content

        # Parse and execute the tool call
        tool_name, input_data = self.parse_tool_call(response)
        if tool_name:
            result = self.mcp_client.execute_tool(tool_name, input_data)
            # Generate final response using tool result
            final = self.llm.generate(
                f"Tool result: {result}\nAnswer the query: {query}"
            ).content
            return final
        return response

An MCPAgent connects to an MCP server, lists available tools, and executes them.

AI prompt: Try it with AI

Loading practice…

Quiz: Quiz

Loading practice…

Matching exercise: Match MCP Components

Loading practice…

Flashcards: Flashcards

Loading practice…

MCP gives your agents a universal way to connect to external tools and services. As the ecosystem grows, your agents will be able to tap into an ever-expanding library of capabilities without writing custom integrations. Next, we will look at how agents can set and monitor their own goals.