> Source: https://ashareapi.com/en/docs/guides/ai-quant-mcp/  ·  Markdown version for LLMs / AI agents

Tutorial

# Using MCP to Do A-Share Quant Research with an AI Agent
 Configure MCP once and the AI Agent fetches quotes, financials and money flow itself in chat; backtesting and adjudication stay with you — here is the full flow with copy-paste code.

## MCP or REST? Split by scenario
 |
| | What you are doing | Use | Why

| | Exploratory questions (what is this stock doing, where is the rotation) | MCP | You talk; the AI Agent calls tools. No fetching code

| | Backtests / batch jobs / scheduled tasks | REST | You control timing, parameters and retries — easier in code

| | Both | Either | MCP for research in chat, REST for production scripts; same data, same key

 Exploratory questions (what is this stock doing, where is the rotation)
 MCP
 You talk; the AI Agent calls tools. No fetching code

 Backtests / batch jobs / scheduled tasks
 REST
 You control timing, parameters and retries — easier in code

 Both
 Either
 MCP for research in chat, REST for production scripts; same data, same key

## Step 1: Connect the data with one command
 Claude Code shown below. Config paths and key-name differences for the other 18 clients (Cursor / VS Code / Codex / CodeBuddy / WorkBuddy / Trae ...) are on the [MCP page](/en/mcp) — each checked against official docs.

 Terminal (add once, works in every later session)
```
claude mcp add --transport http ashareapi https://api.ashareapi.com/mcp

# Free tools need no key; for paid tools add one header:
# "headers": { "Authorization": "Bearer <your key>" }
```
 Claude Code · .mcp.json

```
{
 "mcpServers": {
 "ashareapi": {
 "type": "http",
 "url": "https://api.ashareapi.com/mcp",
 "headers": { "Authorization": "Bearer ct-your-key" }
 }
 }
}
```
 Cursor · .cursor/mcp.json

```
{
 "mcpServers": {
 "ashareapi": {
 "url": "https://api.ashareapi.com/mcp",
 "headers": { "Authorization": "Bearer ct-your-key" }
 }
 }
}
```
 Codex · ~/.codex/config.toml

```
[mcp_servers.ashareapi]
url = "https://api.ashareapi.com/mcp"
http_headers = { Authorization = "Bearer ct-your-key" }
```

## Step 2: Give the AI Agent work it is actually good at
 Models are good at **bulk, structured, repetitive** work — not at "predicting price". These are the things worth handing over:

 Prompt example (edit freely)
```
Using the tools, pull the last 60 daily bars for sh600667 and compute:
1) Close deviation from MA20 (%)
2) Last 5-day turnover vs the prior 20-day average
3) List dates where deviation and volume both expanded
Data and calculation only -- no buy or sell advice.
```

-
 **Bulk screening**: list the constituents of five concept sectors with their change, flag the top 10 by main-force net inflow

-
 **Reading filings**: compress three announcements into five bullet points, marking the direction of impact

-
 **Indicator maths**: pull the last 60 daily bars, compute MA20 and volume ratio, describe the position (no forecast)

-
 **Cross-checking**: align two sources and point out mismatched fields and likely causes

## Step 3: Write the backtest yourself (the AI does not validate for you)
 "Let the AI write the backtest code" is fine; "let the AI tell you the strategy works" is not — it does not know your cost assumptions, sample boundaries or bias sources. A minimal skeleton:

 Python (backtest skeleton)
```
import requests

def kline(code, n=250):
 r = requests.get("https://api.ashareapi.com/v1/kline",
 params={"code": code, "period": "day", "count": n}, timeout=10)
 return list(reversed(r.json()["data"])) # ascending by date (API returns newest first)

rows = kline("sh600667")
# 1) Write the rule (e.g. close > MA20 and volume ratio > 1.5, hold next day)
# 2) Compute net returns: subtract round-trip cost (10cm about -0.50% / 20cm about -1.10%)
# 3) Split: define on the first 70%, look at the last 30% only (discard if direction flips)
```

## Step 4: Adjudicate with four rules (all four required)

-
 **Enough samples**: n >= 30 — a tiny sample of wins has no statistical meaning

-
 **Confidence interval**: 95% lower bound of the expectation above 0 (bootstrap or t-test; if it contains 0, keep accumulating)

-
 **Out-of-sample**: define on the first 70%, adjudicate on the last 30%; the direction must agree

-
 **Net first**: it must stay positive after round-trip costs to count as a strategy

## Free tiers and limits (stated plainly)

-
 **Five data endpoints are free and need no key**: quote / K-line / hot list / market overview / up-down distribution

-
 Anonymous rate limit **5/min**; one PoW challenge (`GET /v1/challenge`) raises it to **60/min**; keys are tiered (Standard 120/min)

-
 **Not provided**: minute bars, full-text news/announcements/reports (research reports are dehydrated summaries only), index daily K-lines — bring another vendor (e.g. Tushare); the two can coexist

-
 Codes need a market prefix: `sh600667` / `sz000001` / `bj8xxxxx` / `hk00700` / `usAAPL`

## No data coming back? Check these five

-
 **Wrong URL**: it must be `https://api.ashareapi.com/mcp` (note the `api.` host and `/mcp` path)

-
 **Missing prefix**: `600667` will not resolve; use `sh600667`

-
 **Shared quota**: a shared egress IP hits the anonymous limit fast — add a key

-
 **Tool list syncs on save**: tools we add later require re-saving the config to appear

-
 **Paid tools without a key**: you get an explicit message, not a silent failure

## FAQ
 Does MCP cost extra, and how is it different from calling the API?
 MCP itself costs nothing extra — it spends the same API quota. The difference is **who writes the code**: with REST you fetch and feed the model; with MCP you configure once and the AI Agent fetches on demand. Same backend (the same 29-endpoint service).

 Can the AI pick stocks or trade automatically?
 Tools can automate fetching, computing and screening; but **stock conclusions and anything touching real money should not run unattended**. This page covers research tooling and validation only, and gives no buy or sell advice.

 How much history do I need for a backtest?
 The K-line endpoint returns a decent number of bars per call (mind the free-tier limits); for long-horizon tests, store the daily data locally instead of refetching. Financials and money flow are daily/filing frequency, enough for medium-term validation.

 Is the data delayed?
 Quotes are same-day real-time snapshots (refreshed during the session); financials, money flow and top-trader boards update per trading day. **Minute bars are out of scope** — use a specialist vendor for intraday.

 Last updated: 2026-09-21
 Setup commands and per-client key names were checked against official docs (2026-09-21); tool counts and the free list come from the live `tools/list` (24 tools, 5 of which need no key); cost magnitudes come from our own backtest convention settings.

 Read next

-
[AI Quant: data and validation (topic page)](/en/docs/ai-quant)

-
[MCP page (config for 19 clients)](/en/mcp)

-
[Getting A-share quotes with Python (three approaches)](/en/docs/guides/python-ashare-quotes)

-
[Endpoint reference](/en/endpoints)

 [← All tutorials](/en/docs/guides)
