Skip to content

feat(mcp): lens_quote — route, depth, impact for one order size (#685) - #809

Open
Neziahtech wants to merge 2 commits into
Divinity-Labs:mainfrom
Neziahtech:fix/agent-trade-cost-685
Open

Neziahtech wants to merge 2 commits into
Divinity-Labs:mainfrom
Neziahtech:fix/agent-trade-cost-685

Conversation

@Neziahtech

Copy link
Copy Markdown
Contributor

Closes #685

The problem

An agent could ask Lens for a price but not for the cost of its own order. Route, order-book depth and price impact each already had a REST route — GET /price/:assetA/:assetB/route, GET /price/:assetA/:assetB/depth, GET /impact/:assetA/:assetB — and MCP had none of them. So an agent doing the one thing agents are actually for ("what does swapping this amount cost?") had to make three HTTP calls and join the answers itself, with no guarantee the three legs agreed on pair, direction or network.

What this adds

One tool, lens_quote, that answers all three legs in one call at the size the caller names. Its result carries the route, the depth ladder and the impact ladder for that amount, plus the resolved pairKey, base/quote, direction and the quote-per-base orientation, so the answer says what it priced and in which direction.

It is the same computation, not a parallel implementation. The legs are the very functions the REST routes call — getBestRoute, getDepth, getImpact — reached through injected reads, and tests/mcp-quote-parity.test.ts drives both surfaces against the same stubs and asserts the bodies are equal and that the arguments handed to the shared pricing code are identical (including the network-scoped ones). Equality is asserted for the reversed pair too: asking XLM/USDC and asking USDC/XLM are the same market, and the direction label says which way round the answer is.

The tool module stays pure. Pair resolution, argument validation and the envelope live in src/mcp/quoteTool.ts; every number arrives through LensQuoteQueries (src/mcp/quoteQueries.ts), which is where the pricing modules are imported. src/mcp/quoteTool.ts imports no database driver, no Prisma, no stellar-sdk, no route module — and a test asserts that boundary by reading the file's own import statements, so it cannot rot into a ../db import later. Reads are injected, as they are for the other tools, and the tool is advertised only when the host can supply them.

Issuer rules are REST's rules. A code that matches while its issuer does not is refused with ERR_ASSET_ISSUER_MISMATCH naming the watched issuer, rather than being priced against a same-named asset on the other network — the same binding findPair applies on the REST side, including that an issuer binds only when both the request and the watched pair name one. An unwatched pair is ERR_PAIR_NOT_WATCHED and lists what is watched; no pricing function is called in either case.

The amount is validated strictly. parseQuoteAmount accepts a positive finite decimal only — no 0, no negatives, no scientific notation, no trailing junk like 10abc — and refuses before any read happens, because for this tool the amount is the question.

A leg that cannot price the order is a qualified result, not a failed call. Depth reporting status: "unavailable" keeps its reason and excluded pools; a pair with no AMM leg to measure returns impact: null; both are also named in unavailable. Only a read that throws becomes ERR_SERVICE_UNREACHABLE, worded "Retry later rather than treating the missing numbers as zero liquidity" — the failure mode that would actually mislead an agent. The same arms are proved against REST: the depth 503 and the impact 404 unknown_pair arrive as qualified parts of the tool's answer, which is what parity means here.

Read-only, and bounded. No tool here writes anything and this one does not either; the response goes through the existing byte budget in src/mcp/limits.ts, and when a payload has to be cut the notice says the depth and impact ladders are the part that was cut. README.md's MCP table gained the lens_quote row and the surrounding paragraph states which REST routes it mirrors and how a leg that cannot price behaves.

How it is proved

  • tests/mcp-quote-parity.test.ts (10 tests) — boots the real Fastify REST routes and the real MCP tool over stubbed pricing legs: bodies equal, call arguments equal, reversed/as-requested equal, mainnet scoping of every leg, depth-unavailable carried as a result, impact: null vs REST's 404, unwatched pair, issuer mismatch, both transports (stdio and HTTP) advertising the same tool identically, and a 401 before any pricing read when API keys are required.
  • tests/mcp-quote-tool.test.ts (24 tests) — the pure half against injected stub reads: the amount-validation matrix, the envelope, each leg's fields matching what the injected read returned, the refusals, the unavailable arms, a throwing read yielding ERR_SERVICE_UNREACHABLE without leaking the driver's message, byte-budget truncation behaving as the other tools do, and the import-boundary assertion above.

Existing MCP tests were not modified or loosened: tests/mcp-lens.test.ts still asserts tool equality with REST for the tools it covers, and the pre-existing tool set, scopes, CORS rules and rate limits are untouched — src/app.ts was not modified at all. Nothing here needs network access: the parity file stubs getBestRoute/getDepth/getImpact and mocks ../db and ../redis the way the existing tests do, and no new price/amount arithmetic was introduced (the legs' numbers are passed through as the pricing layer produced them, so no new parseFloat/Number() path appears).

Out of scope

No new REST route, no OpenAPI change (the tool surfaces through MCP, and tools/list is not in the spec), no schema change. Adding the other Drips-tool candidates (volumes, spreads) is left to their own issues — two PRs already open for those.

An agent could ask Lens for a price but not for the cost of *its* order.
Route, depth and impact answered that in REST across three routes and in
MCP not at all, so the agent had to make three HTTP calls and join them
itself.

lens_quote answers all three legs in one call, at the order size the caller
names: it runs the very functions GET /price/:assetA/:assetB/route,
GET /price/:assetA/:assetB/depth and GET /impact/:assetA/:assetB run, and
tests/mcp-quote-parity.test.ts drives both surfaces against the same stubs
and asserts the bodies are equal and the arguments handed to the shared
pricing code are identical.

The tool module stays pure: pair resolution, argument validation and the
envelope live in src/mcp/quoteTool.ts, and every number arrives through the
injected LensQuoteQueries (src/mcp/quoteQueries.ts), so nothing behind the
tool reaches a database driver. Issuer rules are REST's rules, and a code
that matches while its issuer does not is refused as
ERR_ASSET_ISSUER_MISMATCH rather than priced against a same-named asset.

A leg that cannot price the order is a qualified result, not a failed call:
depth carries status "unavailable" with its reason and excludedPools, impact
is null, and both appear in `unavailable`. Only a read that throws becomes
ERR_SERVICE_UNREACHABLE, worded so it cannot be read as zero liquidity.
@drips-wave

drips-wave Bot commented Nov 23, 2026

Copy link
Copy Markdown

@Neziahtech Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits.

You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀

Learn more about application limits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

An agent can read a price but cannot ask what its trade would cost

1 participant