Repository navigation
feat(mcp): lens_quote — route, depth, impact for one order size (#685) - #809
Open
Neziahtech wants to merge 2 commits into
Open
Neziahtech wants to merge 2 commits into
Neziahtech wants to merge 2 commits into
Conversation
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.
|
@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! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 resolvedpairKey,base/quote,directionand thequote-per-baseorientation, 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, andtests/mcp-quote-parity.test.tsdrives 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: askingXLM/USDCand askingUSDC/XLMare the same market, and thedirectionlabel 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 throughLensQuoteQueries(src/mcp/quoteQueries.ts), which is where the pricing modules are imported.src/mcp/quoteTool.tsimports no database driver, no Prisma, nostellar-sdk, no route module — and a test asserts that boundary by reading the file's own import statements, so it cannot rot into a../dbimport 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_MISMATCHnaming the watched issuer, rather than being priced against a same-named asset on the other network — the same bindingfindPairapplies on the REST side, including that an issuer binds only when both the request and the watched pair name one. An unwatched pair isERR_PAIR_NOT_WATCHEDand lists what is watched; no pricing function is called in either case.The amount is validated strictly.
parseQuoteAmountaccepts a positive finite decimal only — no0, no negatives, no scientific notation, no trailing junk like10abc— 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 returnsimpact: null; both are also named inunavailable. Only a read that throws becomesERR_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 404unknown_pairarrive 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 thelens_quoterow 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: nullvs 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, theunavailablearms, a throwing read yieldingERR_SERVICE_UNREACHABLEwithout 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.tsstill 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.tswas not modified at all. Nothing here needs network access: the parity file stubsgetBestRoute/getDepth/getImpactand mocks../dband../redisthe 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 newparseFloat/Number()path appears).Out of scope
No new REST route, no OpenAPI change (the tool surfaces through MCP, and
tools/listis 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.