APAaryan Patel

case study · quant tooling · Python · Model Context Protocol

edgar-mcp

Give a model SEC filings and XBRL facts as tools, and stay a polite client of a government API while it hammers them.

requests the first version sent in a single second, against a ceiling of 10
19
the peak in any one-second window once the pacer replaced the bucket
10
faster on a warm 10-K read than a cold one, measured
138×
tests, including the one that caught the burst
32
simulation of both limiters · the 19 and the 10 are measured

A token bucket is not a rate limit.

A bucket of 10 refilling at 10 per second averages 10/s, but it starts full. A burst spends all 10 tokens at once and then keeps taking one every 100 ms, so the first second carries 19. The SEC counts per second, not on average. The fix spaces every request at least ~104 ms after the last.

workload
0token bucket: most in any 1 s
0pacer: most in any 1 s
0bucket, in the highlighted second
0pacer, in the highlighted second
measured · cache, cold against warm

how it works

Eight tools, one polite client.

The server exposes lookup_company, list_filings, get_filing_text, list_concepts, get_concept, compare_concept, search_filings and cache_stats. Every one of them goes through the same pacer and the same on-disk cache.

The bug a test caught

The first benchmark run reported 19 requests inside one second, and a 3.75 MB "cache hit" that was really a full re-download. A class named RateLimiter or Cache proves nothing by itself, so the benchmark now counts grants inside a real one-second window and bytes on the wire.

Filings a model can read

Inline XBRL is stripped to text at 23.4 MB/s. A 10-K is still around 206K characters, so it's served in 40K windows the model can page through instead of one context-filling blob.

Facts, not prose

XBRL company facts answer "what was revenue in FY2023" from structured data, and compare_concept lines the same concept up across companies.

measured · Apple M-series · Python 3.13 · live EDGAR
Cache, cold against warm
requestcoldwarmgain
companyfacts (3.75 MB)97 ms1.4 ms68×
10-K text (1.52 MB)154 ms1.1 ms138×
submissions–0.2 ms755×
Rate limiting against the SEC's 10 req/s
limitersustainedpeak in 1 s
token bucket, capacity 10~10 req/s19
fixed-interval pacer9.2 req/s10
where it stops holding

The known gaps, written down.

  • Freshness is a guessdata.sec.gov sends no ETag or Last-Modified, so freshness falls back to a 1-hour TTL. A filing that lands mid-TTL is invisible until it expires.
  • Windows re-readEDGAR honors HTTP Range, but partial HTML can't be parsed reliably, so the whole document is fetched once and windowed from cache. The first call on a large filing pays the full download.
  • Search isn't cachedResults are query-shaped and the endpoint offers no validators, so every search is a live round trip against the 9 req/s budget. It also only reaches back to 2001.
  • One pacer for everythingTen companies queried in parallel serialize at ~9 req/s. Correct, not fast. And XBRL values are consolidated totals: segment and geography breakdowns aren't surfaced.
reproduce
git clone https://github.com/asp53826/edgar-mcp
cd edgar-mcp
make test    # 32 tests, no network
export EDGAR_USER_AGENT="your-project you@example.com"
make bench   # live EDGAR, prints the table