Skip to content

Separate MCP query logic from tool handler serialization#653

Draft
st0012 wants to merge 3 commits intomainfrom
separate-mcp-query-logic-from-handlers
Draft

Separate MCP query logic from tool handler serialization#653
st0012 wants to merge 3 commits intomainfrom
separate-mcp-query-logic-from-handlers

Conversation

@st0012
Copy link
Member

@st0012 st0012 commented Mar 6, 2026

Summary

  • Extract core query logic from server.rs tool handlers into a new queries.rs module with typed Rust result structs, enabling direct testing of business logic without JSON serialization
  • Refactor all 6 MCP handlers to thin adapters (~5 lines each): acquire graph → normalize params → call query → serialize
  • Split tests into two layers: typed unit tests in queries.rs for business logic, JSON shape + error tests in server.rs for the MCP integration layer
  • Remove intermediate Vec allocations in pagination by using iterator .count() + .filter().skip().take()
  • Use &'static str for kind fields and HashMap keys instead of String to eliminate unnecessary allocations

Changes

File Lines What
queries.rs (new) 923 QueryError enum, 12 typed result structs, 6 query functions, 22 unit tests
server.rs 569 (was 1134) Thin handlers, 13 integration tests (6 JSON shape + 5 error + 2 server state)
main.rs +1 mod queries;

st0012 added 3 commits March 6, 2026 21:19
Add offset/total pagination to search_declarations, get_descendants,
and find_constant_references. Uses a two-pass paginate! macro: filter
pass counts total, map pass builds JSON only for the requested page.

- search_declarations: response wrapped in {results, total} envelope
- get_descendants: gains limit (default 100, max 500) and offset
- find_constant_references: gains offset; total always present
- limit=0 treated as default to guard against accidental empty pages
- Tool descriptions and server instructions mention pagination
- Add GraphTest::into_graph() for test graph injection
- Add 26 unit tests calling tool handlers directly (no process spawn)
- Windows-compatible test URIs via test_root()/test_uri() helpers
Add MCP Server section covering the two-pass pagination approach,
result ordering guarantees (stable within a session, may vary across
restarts due to parallel indexing), and key file references.
Extract core query logic from server.rs tool handlers into a new
queries.rs module with typed Rust result structs. This enables testing
business logic directly without JSON serialization.

- Add queries.rs with QueryError enum, 12 typed result structs, 6 pure
  query functions, and 22 unit tests
- Refactor server.rs handlers to thin adapters (~5 lines each): acquire
  graph, normalize params, call query, serialize
- Split tests: query logic tests in queries.rs (typed), JSON shape and
  error tests in server.rs (integration)
- Remove intermediate Vec allocations in pagination (use iterator count)
- Use &'static str for kind fields and HashMap keys instead of String
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.

1 participant