Guide · Agent builders

Use Chart Library with LangChain & LangGraph
— cohort tools in a few lines.

Chart Library ships an MCP server, so adding its historical pattern tools to a LangChain / LangGraph agent takes one adapter and a few lines. Your agent gets the 13 canonical tools that return the cohort of historically similar setups and what they did next — grounded outcomes instead of generated technical analysis.

This walks from clean install to a working ReAct agent in about ten minutes.

Step 1: Install

You need the MCP server itself plus the LangChain MCP adapter and a LangGraph runtime:

pip install chartlibrary-mcp langchain-mcp-adapters langgraph langchain-anthropic

The chartlibrary-mcp command speaks the standard Model Context Protocol over stdio, and langchain-mcp-adapters turns those MCP tools into native LangChain tools.

Step 2: Get an API key

Sign up at chartlibrary.io/developers for a free Sandbox key (1,000 calls/day across search, context, explain, and the full cohort intelligence). Cohort intelligence (cohort_analyze, cohort_introspect, and the handover surface) is free — Builder tier adds volume, not access. Export the key — note the exact variable name, CHART_LIBRARY_API_KEY:

export CHART_LIBRARY_API_KEY=cl_your_key_here

Step 3: Load the tools and build the agent

MultiServerMCPClient launches the server as a subprocess and discovers its tools. Hand them to create_react_agent and you have a working agent:

import asyncio
import os

from langchain_mcp_adapters.client import MultiServerMCPClient
from langgraph.prebuilt import create_react_agent
from langchain_anthropic import ChatAnthropic


async def main():
    client = MultiServerMCPClient(
        {
            "chartlibrary": {
                "command": "chartlibrary-mcp",
                "args": [],
                "transport": "stdio",
                "env": {
                    "CHART_LIBRARY_API_KEY": os.environ["CHART_LIBRARY_API_KEY"],
                },
            }
        }
    )

    tools = await client.get_tools()  # 13 canonical tools (incl. handover surface)

    agent = create_react_agent(
        ChatAnthropic(model="claude-sonnet-4-6"),
        tools,
    )

    result = await agent.ainvoke(
        {
            "messages": [
                {
                    "role": "user",
                    "content": (
                        "What did setups similar to NVDA on 2024-08-05 (1h) "
                        "do next? Call search to anchor the cohort, then "
                        "cohort_analyze, and cite the median 5-day return "
                        "and win rate."
                    ),
                }
            ]
        }
    )
    print(result["messages"][-1].content)


asyncio.run(main())

The agent will call search then cohort_analyze (and possibly context or cohort_introspect), then answer from the returned distribution rather than from training data.

Step 4: Keep it grounded with a system prompt

The tools return historical facts; the model still chooses how to use them. A short system prompt prevents it from drifting into predictions:

SYSTEM = (
    "You are a market-analysis agent. For any question about a stock, "
    "use the Chart Library tools to retrieve the cohort of historically "
    "similar setups and what they did next. Report the distribution "
    "(median, win rate, p10/p90) as historical fact. Never predict a "
    "single price target. If the cohort is small (n < 30), say so."
)

agent = create_react_agent(
    ChatAnthropic(model="claude-sonnet-4-6"),
    tools,
    prompt=SYSTEM,
)

For LangGraph users building multi-step research loops, the same tools drop into any node — the cohort cohort_id can be threaded between steps so you retrieve once and analyze many times.

See it in a full crew

The snippet above is the node on its own. For a complete, runnable example — this same cohort node wired alongside fundamentals, technicals, news, macro, and risk specialists in a LangGraph StateGraph that plans, fans out to the specialists in parallel, and synthesizes one grounded brief — see ports/langgraph_crew.py in the open-source agent-crew reference app. It ships with an eval harness that measures whether the orchestrator actually reaches for the cohort node, plus a with/without A/B on answer quality.

Frequently asked questions

Do I need a paid key to try this?
No. The free Sandbox tier covers search, context, explain, and the flagship cohort intelligence at 1,000 calls/day. cohort_analyze, cohort_introspect, and the full-cohort handover surface are all free, keyless or with a free key. Paid tiers add volume, higher RPM, and SLA, not feature unlocks.
Which tools does the agent see?
The 13 canonical tools advertised on tools/list: search, pull_comps (front-of-house for cohort_analyze), cohort_introspect, cohort_members, cohort_groupby, cohort_rerank, symbol_intelligence, analyze, context, explain, portfolio, track_record, and report_feedback. The core loop is search → pull_comps → cohort_introspect, and the retrieval tools are annotated readOnlyHint=true so MCP clients know they never mutate state.
Does this work with OpenAI models instead of Claude?
Yes — swap ChatAnthropic for any LangChain chat model (e.g. ChatOpenAI). The tools are model-agnostic. There is also a dedicated OpenAI Agents SDK guide.
Try it

Run a cohort_analyze call.

Free Sandbox tier — 1,000 calls/day, no authentication. MCP install for Claude or Cursor takes 30 seconds.

Related