In today’s markets, where billions are made in seconds, waiting hours for stock analysis is no longer viable. Imagine typing “plot me stock gains of X” and instantly receiving a clean chart. No manual data collection, coding, or debugging. In this guide, we’ll build a Personal Market Analyst using CrewAI and MCP. By the end, you’ll have an assistant that transforms natural language queries into actionable stock insights.
Our MCP-powered financial analyst streamlines financial data analysis using AI agents. It bridges the gap between complex financial data sources and user queries, delivering real-time, context-aware insights. This approach not only saves time and reduces manual errors but also ensures better security, interoperability, and transparency.

Here are some necessary Python packages
pip install crewai crewai-tools pydantic yfinance python-dotenv
We will be building out two main components:
Let’s break it down.
Here’s where the real intelligence happens. We are using CrewAI to coordinate multiple agents with different roles:
class QueryAnalysisOutput(BaseModel):
symbols: list[str]
timeframe: str
action: str
This Pydantic model ensures structured extraction from the user’s query.
llm = LLM(
model="openai/gpt-4o",
temperature=0.7
)
Agent 1: Query Parser
query_parser_agent = Agent(
role="Stock Data Analyst",
goal="Extract stock details and fetch required data...",
output_pydantic=QueryAnalysisOutput
)
Agent 2: Code Writer
code_writer_agent = Agent(
role="Senior Python Developer",
goal="Write Python code to visualize stock data..."
)
Agent 3: Code Executor
code_execution_agent = Agent(
role="Senior Code Execution Expert",
allow_code_execution=True,
allow_delegation=True
)
We will follow a sequence while using these agents, i.e.:
crew = Crew(
agents=[query_parser_agent, code_writer_agent, code_execution_agent],
tasks=[query_parsing_task, code_writer_task, code_execution_task],
process=Process.sequential
)
Orchestrates the entire multi-agent system and returns the final executable Python code.
def run_financial_analysis(query):
result = crew.kickoff(inputs={"query": query})
return result.raw
Now we will create the MCP Server File:
from mcp.server.fastmcp import FastMCP
from finance_crew import run_financial_analysis
FastMCP: A lightweight MCP server framework for exposing functions as AI tools
run_financial_analysis: The main function from the above code that does all the heavy lifting.
We name our MCP toolset as “financial-analyst”. This acts like the app name.
mcp = FastMCP("financial-analyst")
Tool 1: analyze_stock()
@mcp.tool()
def analyze_stock(query: str) -> str:
...
result = run_financial_analysis(query)
return result
Tool 2: save code
Saves the generated Python code into a file, stock_analysis.py
Ensures the file is valid and executable.
@mcp.tool()
def save_code(code: str) -> str:
with open('stock_analysis.py', 'w') as f:
f.write(code)
Tool 3: run_code_and_show_plot()
Runs the MCP server locally over stdio, ready to integrate into any AI platform that supports MCP.
if __name__ == "__main__":
mcp.run(transport='stdio')
With this setup, typing a query like “Plot Apple’s stock performance for the last 6 months” produces a ready-to-use chart – no manual coding required.
Also Read: How to Create an MCP Client Server Using LangChain
Building a MCP-powered Financial Analyst with CrewAI shows how multi-agent systems can transform financial analysis. By combining structured query parsing, automated Python code generation, and real-time execution, we eliminate the bottlenecks of manual coding and debugging. This project highlights how AI agents can handle complex workflows efficiently and sets a new benchmark for user-focused financial analytics.
If you want to learn the basics of MCP, enroll in our FREE course: Foundations of Model Context Protocol
A. MCP-powered financial analyst uses Model Context Protocol and CrewAI agents to automate financial data analysis, transforming natural language queries into actionable stock insights instantly.
A. CrewAI coordinates multiple AI agents that parse queries, write Python code, and execute it. This enables real-time, automated stock analysis without manual coding or data preparation.
A. Yes, MCP integrates smoothly with financial workflows by exposing AI tools like analyze_stock and run_code. It enhances interoperability, automation, and security for financial data analysis pipelines.
A. AI agents reduce manual errors, accelerate insights, and provide real-time, data-driven analysis. They automate repetitive financial tasks, helping analysts focus on decision-making and strategy rather than coding.