Connecting Tools with MCP

Updated · 5 min read
hardPython 3.13aiagentsmcptoolspython

The Model Context Protocol (MCP) is an open standard for connecting AI applications to tools and data. Instead of writing a separate integration for every app, you write one MCP server that exposes tools; any MCP client, such as an AI assistant, a coding tool or your own program, can discover and use them. Anthropic introduced MCP in November 2024, and it is now supported by many AI applications.

Think of MCP like a standard plug: build the plug once, and it fits every socket that supports the standard.

Servers and Clients

Build the tool once; use it from any MCP client
PartDescription
MCP serverIt is used to expose tools, resources and prompts in the standard format
MCP clientIt is used inside an AI application to discover and call a server's tools
ToolsActions the model can call, such as look_up_fee
ResourcesData the application can read, such as documents or records
PromptsReusable prompt templates the server offers
TransportHow they talk: standard input and output for local servers, HTTP for remote ones

Example: A Real MCP Server and Client

This lesson uses the official MCP Python SDK, version 2 (pip install mcp). Save the server as campus_server.py:

Python
from mcp.server.mcpserver import MCPServer
 
server = MCPServer("campus-helper")
FEES = {(10, "lab"): 45, (10, "field trip"): 30, (11, "lab"): 50, (11, "field trip"): 35}
 
@server.tool()
def look_up_fee(grade: int, item: str) -> str:
    """Look up a school fee by grade (9-12) and item, such as lab or field trip."""
    amount = FEES.get((grade, item.lower()))
    return f"${amount}" if amount is not None else "No fee found for that grade and item."
 
if __name__ == "__main__":
    server.run()  # talks to the client over standard input and output

Then run this client in the same folder. It starts the server, lists its tools and calls one:

Python
import asyncio
import sys
 
from mcp import Client, StdioServerParameters
 
async def main():
    server = StdioServerParameters(command=sys.executable, args=["campus_server.py"])
    async with Client(server) as client:
        tools = await client.list_tools()
        for tool in tools.tools:
            print("Tool:", tool.name, "|", tool.description)
            print("Inputs:", list(tool.input_schema["properties"]))
        result = await client.call_tool("look_up_fee", {"grade": 11, "item": "lab"})
        print("Result:", result.content[0].text)
 
asyncio.run(main())
Output
Tool: look_up_fee | Look up a school fee by grade (9-12) and item, such as lab or field trip.
Inputs: ['grade', 'item']
Result: $50

Explanation: @server.tool() turns an ordinary function into an MCP tool: the name, description and input schema come from the function's name, docstring and type hints. server.run() serves it over standard input and output. The client launches the server as a separate process, discovers its tools with list_tools() without knowing them in advance, and calls one with call_tool(). Any MCP-capable AI application could use this same server.

MCP vs Writing Tools Directly

Tools in your own codeMCP server
Used byOne applicationAny MCP client
DiscoveryYou list them in each requestClients ask the server
Best forA single app with private toolsTools shared across apps and teams

Common Mistakes

Installing servers from unknown sources. Review what a server can access before connecting it.

Exposing write tools without confirmation. MCP standardizes the connection, not the safety rules; the app must still confirm actions.

Vague docstrings. They become vague tool descriptions in every client.

Interview Corner

What is the Model Context Protocol?

An open standard that lets AI applications discover and use tools, resources and prompts from MCP servers, so one integration works across many apps.

What are the security risks of MCP servers?

A server can run code, access data, and put text in front of the model through its tool descriptions and results. Only connect trusted servers, limit their permissions, and confirm actions.

Practice

Exercise: a second tool. Add next_exam(subject: str) -> str to the server with a docstring. Run the client again: does it discover both tools without any change to the client code?

Check your understanding:

  1. What does an MCP server expose?
  2. Where do the tool's name and description come from in this example?
  3. Why does the client not need to know the tools in advance?

Quick check: MCP

4 questions
1What is MCP?
2Where did the tool's description come from?
3How did the client learn which tools exist?
4What should you do before connecting an MCP server?

Key Takeaways

Quick reference

NeedCode
Create a serverserver = MCPServer("name")
Add a tool@server.tool() on a function
Run itserver.run()
Connectasync with Client(StdioServerParameters(...)) as client:
Discover and callawait client.list_tools(), await client.call_tool(name, args)

Was this tutorial helpful?

Lesson 105 of 186