In the previous chapter, Multi-Agent Orchestration, we built a team of specialized agents that can research, analyze, and write reports.
But currently, your agents live in a "black box"โa terminal window or a Python script. To use them, you have to run code. What if you want to use your financial analyst inside a beautiful, polished application like Claude Desktop? What if you want to drag-and-drop a PDF into Claude and have your custom code analyze it?
This chapter introduces the bridge between your code and the rest of the world: The Model Context Protocol (MCP).
Imagine you built a custom engine.
MCP allows your local Python tools to appear as "native features" inside AI applications. It turns your script into a server that apps can talk to.
MCP works on a simple relationship:
Think of it like a restaurant:
The Waiter doesn't know how to cook a steak; they just know how to send the order to the Kitchen.
We will use the FastMCP library, which makes creating these servers incredibly easy. We are looking at the file finagent/main.py.
We create an instance of a server. This is like opening the restaurant kitchen.
from mcp.server.fastmcp import FastMCP
from financial_agents import run_financial_analysis
# Create the server instance
# "financial-analyst" is the name Claude will see internally
mcp = FastMCP("financial-analyst")
This is the most important part. We have a complex function run_financial_analysis from our previous chapters. We need to tell the MCP server: "Allow the outside world to call this."
We use the @mcp.tool() decorator to do this.
@mcp.tool()
def analyze_stock(query: str) -> str:
"""
Performs a natural language stock market analysis.
Returns a summary and recommendations.
"""
try:
# We call our complex agent logic here
result = run_financial_analysis(query)
return result
except Exception as e:
return f"Error: {e}"
Crucial Detail: The docstring ("""...""") is NOT just a comment. The Host (Claude) reads this to understand what the tool does and when to use it.
Finally, we tell the script to listen for connections.
if __name__ == "__main__":
# 'stdio' means it talks over standard input/output
# This is how local desktop apps communicate securely
mcp.run(transport='stdio')
How does Claude Desktop actually talk to your Python script? They use a standard communication flow over stdio (Standard Input/Output).
When you ask Claude a question, it doesn't immediately answer. It checks its list of tools first.
Writing the code in finagent/main.py is only half the battle. We now need to tell Claude Desktop where to find this server.
This is done via a configuration file on your computer (usually claude_desktop_config.json).
You don't write this in Python; you add this to your Claude settings.
{
"mcpServers": {
"financial-analyst": {
"command": "python",
"args": [
"/absolute/path/to/finagent/main.py"
],
"env": {
"GEMINI_API_KEY": "your_key_here"
}
}
}
}
What is happening here?
command: We tell Claude to run python.args: We give it the path to our main.py.env: We pass our API keys (so our agent can access Gemini).Once this is saved and you restart Claude Desktop:
In this chapter, we learned:
Now we have a text-based system that can look up data, analyze it, and present it in a nice interface. But financial analysis isn't just about text. It's about charts, graphs, and visual trends.
In the next chapter, we will learn how to make our AI understand and generate images.
๐ Next Step: Multimodal Processing
Generated by Code IQ