Connecting Tools with MCP
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
| Part | Description |
|---|---|
| MCP server | It is used to expose tools, resources and prompts in the standard format |
| MCP client | It is used inside an AI application to discover and call a server's tools |
| Tools | Actions the model can call, such as look_up_fee |
| Resources | Data the application can read, such as documents or records |
| Prompts | Reusable prompt templates the server offers |
| Transport | How 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:
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 outputThen run this client in the same folder. It starts the server, lists its tools and calls one:
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())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: $50Explanation: @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 code | MCP server | |
|---|---|---|
| Used by | One application | Any MCP client |
| Discovery | You list them in each request | Clients ask the server |
| Best for | A single app with private tools | Tools 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:
- What does an MCP server expose?
- Where do the tool's name and description come from in this example?
- Why does the client not need to know the tools in advance?
Key Takeaways
Quick reference
| Need | Code |
|---|---|
| Create a server | server = MCPServer("name") |
| Add a tool | @server.tool() on a function |
| Run it | server.run() |
| Connect | async with Client(StdioServerParameters(...)) as client: |
| Discover and call | await client.list_tools(), await client.call_tool(name, args) |
Was this tutorial helpful?