Use Chart Library with the OpenAI Agents SDK
— real history, not hallucinated TA.
The OpenAI Agents SDK speaks MCP natively, so the Chart Library server plugs in directly. Your agent gets the 13 canonical tools — the core loop is search → cohort_analyze → cohort_introspect — that return the cohort of historically similar setups and their forward outcomes, so “is NVDA bullish here” gets answered with verified base rates, not invented analysis.
From clean install to a working agent in about ten minutes.
Step 1: Install
pip install chartlibrary-mcp openai-agentschartlibrary-mcp is the MCP server (it speaks the standard protocol over stdio); openai-agents is the OpenAI Agents SDK, which can launch and consume MCP servers.
Step 2: Get an API key
Sign up at chartlibrary.io/developers for a free Sandbox key. Export it under the exact variable name CHART_LIBRARY_API_KEY:
export CHART_LIBRARY_API_KEY=cl_your_key_hereYou will also need OPENAI_API_KEY set for the model itself.
Step 3: Launch the server and run the agent
MCPServerStdio launches chartlibrary-mcp as a subprocess and exposes its tools to the agent. Use it as an async context manager:
import asyncio
import os
from agents import Agent, Runner
from agents.mcp import MCPServerStdio
async def main():
async with MCPServerStdio(
params={
"command": "chartlibrary-mcp",
"args": [],
"env": {
"CHART_LIBRARY_API_KEY": os.environ["CHART_LIBRARY_API_KEY"],
},
},
cache_tools_list=True,
) as chartlibrary:
agent = Agent(
name="Chart Research Agent",
instructions=(
"For any stock question, 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."
),
mcp_servers=[chartlibrary],
)
result = await Runner.run(
agent,
"What did setups similar to NVDA on 2024-08-05 (1h) do next? "
"Call search to anchor the cohort, then cohort_analyze.",
)
print(result.final_output)
asyncio.run(main())cache_tools_list=True avoids re-fetching the tool list on every turn — the surface is stable, so caching is safe and faster.
Why ground the agent this way
Language models are confident but unreliable on specific technical-analysis claims — they will happily invent a win rate. Chart Library returns the actual cohort of analogs and their realized outcomes, all annotated readOnlyHint=true, so the agent can cite verified history. Pair this with the system instruction above and the agent stops guessing and starts reporting.
For ChatGPT-side usage (adding Chart Library as a connector in ChatGPT itself), you would point it at a hosted HTTP endpoint rather than the stdio package — that path is on the roadmap.
See it in a full crew
The agent above calls the node on its own. For a complete, runnable example — this same cohort node exposed as @function_tools on an OpenAI Agents SDK agent that orchestrates fundamentals, technicals, news, macro, and risk specialists — see ports/openai_agents_crew.py in the open-source agent-crew reference app. It’s the cross-vendor proof — an OpenAI model deciding when to reach for the Chart Library node — and ships with an eval harness that scores how often it does.
Frequently asked questions
- Do I need a paid Chart Library key?
- No. The free Sandbox tier (1,000 calls/day) covers search, context, explain, and the flagship cohort intelligence — 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 get?
- 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.
- Can I use this with LangChain instead?
- Yes — see the dedicated LangChain & LangGraph guide. The MCP server is framework-agnostic.
Run a cohort_analyze call.
Free Sandbox tier — 1,000 calls/day, no authentication. MCP install for Claude or Cursor takes 30 seconds.