← All work
MCP server · liveLive

Casabourse Lens

Ask an assistant what the Moroccan market is doing and have it answer from the exchange itself — quotes, technicals, fundamentals, macro, FX and corporate actions, through one connection.

27
tools exposed over MCP
80
listed stocks covered
10s–24h
cache freshness, tuned per dataset

What it is

The Casablanca exchange publishes no developer API worth the name. Its data is spread across a broker's internal endpoints, the central bank, the market regulator's filings, and commodity and FX feeds — each with its own shape, none built to be queried in a sentence.

Lens collapses that into one tool surface an assistant can use directly, covering the MASI and MADEX indices, individual listings, and portfolio and sector-performance analysis, with a WebSocket layer underneath for data that moves during the session. You connect it once, then ask in plain language: what the market did today, how a stock's momentum looks, which companies report next, what the policy rate is doing to the banks.

It exists because nothing like it did: when I tried to use AI to trade Moroccan stocks, there was no AI-ready data source for them. Once published it was widely shared and praised by Morocco's investor community.

The design decision that matters

Every tool talks to a provider interface, never to a source. The server is the only component that knows which concrete provider is wired in, so a data source can be replaced without touching a single tool definition — and the assistant never notices.

Caching sits beside that as its own layer rather than inside the providers. Each kind of data carries its own freshness: seconds for quotes, ten seconds for the order book, hours for macro series, a day for company profiles. A cold upstream degrades to slightly stale data instead of failing the question.

  • The exchange's API serves only a leaf certificate, so the intermediate is bundled and loaded alongside certifi rather than disabling verification.
  • Retries use exponential backoff on 5xx and transport errors only — a 404 raises immediately, because it isn't transient.
  • Corporate actions are stored separately so price history can be back-adjusted for splits and dividends.

Data an LLM can trust

Free price data is unadjusted, so any indicator computed across a dividend date lies: a dividend looks like a drop in price, and a moving average or a momentum reading inherits the error.

So the trust layer is built in rather than assumed. A corporate-actions store feeds a reconciler, which settles what the sources say about dividends, and an adjuster, which applies the result to the price history before any indicator reads it. The indicators themselves are computed in-house, in code, rather than left to the model.

Sources are ranked per field, with an explicit precedence, and conflicts between them are marked DISPUTED. Estimates ship with their uncertainty: beta comes with a 95% confidence interval.

Letting an agent trade, safely

An agent simulator was added later: a playground where the agent trades and backtests strategies safely. Order-placing tools only register when they are explicitly configured, so a default install can read the market but cannot trade it.

Running it in public

The same server runs two ways: over stdio on a laptop, and as a public HTTP endpoint. Public changes the threat model, so the public build serves only market-data tools — filtered by where each tool is defined, not by its name, so a stray registration can never publish the local-file or account-trading tools.

On top of that: per-client rate limiting that reads forwarded headers only from a proxy it trusts, argument size limits so cache keys can't be flooded, request body caps, and refusal of unexpected Host headers. A security review of this code produced fixes for all four before it shipped.

Built with

  • Python
  • MCP SDK
  • Starlette
  • WebSockets
  • DuckDB
  • SQLite
  • Vercel

Add https://casabourse-lens.vercel.app/mcp as a custom connector in Claude to use it. It serves read-only market data.