Skip to main content
This cookbook demonstrates how to build a real-time stock analysis and investment planning assistant using LangChain, Dappier MCP, and the lightweight **mcp-use** client. By integrating live financial data through the Model Context Protocol (MCP), this guide walks through constructing agents that deliver in-depth market insights and actionable strategies. In this cookbook, you’ll explore:
  • LangChain + OpenAI: A modular framework to build LLM-powered applications with support for agents, tools, and memory.
  • Dappier MCP: A Model Context Protocol server that connects your agents to real-time, rights-cleared, AI-powered tools such as financial news, live stock data, and market summaries.
  • mcp-use: A lightweight Python client that bridges any LLM with any MCP server using standard transports like stdio and httpβ€”without relying on proprietary SDKs.
  • Stock Analysis & Investment Strategy: A real-world use case where the assistant performs deep research on a specific stock ticker and generates a strategic investment plan based on current market data.
This example demonstrates a flexible architecture for building real-time, tool-augmented assistants and lays the foundation for other real-world applications powered by dynamic context, tool use, and AI reasoning.

πŸ“¦ Installation

To get started, install the required tools and dependencies:
Step 1: Install uv (required to run the Dappier MCP server) macOS / Linux:
Windows:

Step 2: Create a Virtual Environment (Recommended) Create and activate a virtual environment to isolate dependencies. macOS / Linux:
Windows:

Step 3: Install Python Packages Install the necessary Python dependencies including LangChain and mcp-use.

πŸ”‘ Setting Up API Keys

You’ll need API keys for both Dappier and OpenAI to authenticate your requests and access tools.
Dappier API Key Visit Dappier to generate your API key. Dappier provides free credits to help you get started. You can set the API key as an environment variable in your terminal:
Or include it in a .env file at the root of your project:

OpenAI API Key Go to OpenAI to retrieve your API key. Set it in your terminal:
Or include it in your .env file:
In your Python script, load the .env file:
Python

βš™οΈ Import Dependencies

Start by importing the required modules to build the stock research agent. This includes components from mcp-use, LangChain, and environment configuration.
Python
These imports enable:
  • Loading API keys from the environment using dotenv
  • Running asynchronous workflows with asyncio
  • Accessing OpenAI models via LangChain
  • Connecting to the Dappier MCP server and executing tool-augmented financial research using mcp-use

πŸ“ Define User Input

We’ll collect the name of the company or stock from the user, which will be used to perform real-time research and analysis.
Python

πŸ›°οΈ Run the Agent with Dappier MCP

This function sets up the MCP agent using mcp-use, formulates the stock research query, and executes it using real-time financial tools provided by the Dappier MCP server.
Python

🚦 Initialize and Launch the Workflow

The main() function collects user input, then launches the asynchronous workflow to run the stock research agent using mcp-use and Dappier MCP.
Python
To start the agent, run the main function using asyncio:
Python

🌟 Highlights

This cookbook has guided you through building a real-time stock research and investment strategy assistant using LangChain, Dappier MCP, and the **mcp-use** Python client. By connecting your agent to live financial tools via MCP, you’ve enabled detailed market analysis and actionable investment recommendations based on up-to-date data. Key components of this workflow include:
  • LangChain + OpenAI: A modular framework for creating LLM-powered applications with agent capabilities.
  • Dappier MCP: A Model Context Protocol server that enables access to real-time, rights-cleared financial tools including stock data, news, and market sentiment.
  • mcp-use: A lightweight open-source client to connect any LLM to any MCP server using stdio or http transports.
This architecture can be extended to build research agents across other financial domains, such as ETF analysis, portfolio monitoring, or earnings prediction using real-time data and tool orchestration.