feat: @searoute-ts/mcp — first-party MCP server for AI agents - #24
Merged
Conversation
…gents First-party Model Context Protocol server so AI agents (Claude Desktop, the claude CLI, etc.) can compute real shortest sea routes instead of guessing, per issue #13. Ships as its own package under examples/mcp-server to keep the MCP SDK out of the core (searoute-ts stays dependency-lean). - Tools: sea_route(origin, destination, options) → distance, duration, passages, GeoJSON; and sea_route_alternatives(origin, destination, k) → up to k distinct routes by canal permutation, sorted by distance. - Thin wrapper over the searoute-ts public API — no new routing logic. Imports searoute-ts/ports so agents can pass UN/LOCODE codes ('CNSHA') or coordinates (pairs with #7). - Handlers are pure and unit-tested (vitest); the stdio entry point wires them into the SDK's McpServer. - README documents the `claude mcp add` snippet and client config; root README gains a "Use from an AI agent (MCP)" section. test: vitest coverage of both tool handlers (port-code and coordinate routing, passages, restrictions lengthening the route, duration from speed, geometry toggle, unknown-port error, and k distinct sorted alternatives). Closes #13
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.
What does this PR do?
Adds a first-party Model Context Protocol server so AI agents (Claude Desktop, the
claudeCLI, etc.) can compute real shortest sea routes directly — answering "how far is Shanghai to Rotterdam by sea, avoiding Suez?" by calling the library instead of guessing. This is the TypeScript analog of the communitysearoute_mcpPython wrapper referenced in #13.Tools (thin wrappers over the existing public API — no new routing logic):
sea_route(origin, destination, options)→ distance, optional duration, canals/straits traversed, detour ratio, and route GeoJSON.sea_route_alternatives(origin, destination, k)→ up tokdistinct routes by canal permutation (baseline / no-Suez / no-Panama …), sorted by distance.Both accept UN/LOCODE port codes (
'CNSHA') as well as[lon, lat]coordinates — the server importssearoute-ts/portsso codes work out of the box (pairs with #7, as the issue suggested).sea_routealso exposesrestrictions,allowArctic,speedKnots,vesselDraftMeters,maxSnapDistanceKm, and anincludeGeometrytoggle.Packaging — kept out of the core
Shipped as its own package (
@searoute-ts/mcp) underexamples/mcp-server/(the issue offered "its own package or a subpath"). This keeps the MCP SDK — which pulls in ~90 transitive packages — entirely out ofsearoute-ts, so the core stays dependency-lean (it depends on the publishedsearoute-ts@^2.2.0). The core package is untouched by this PR apart from a README section, so the root build/lint/test are unaffected.Structure
src/tools.ts— zod input schemas + pure, testable handlers (runSeaRoute,runSeaRouteAlternatives) returning the MCP result shape; maps library errors (UnknownPortError,NoRouteError,SnapFailedError) toisErrorresults.src/server.ts—createServer()registers the two tools on the SDK'sMcpServer.src/index.ts— stdio entry point (bin: searoute-ts-mcp).README.md— tool reference +claude mcp add/ client-config snippets; root README gains a "Use from an AI agent (MCP)" section.Checklist
Validation
Sub-package (
examples/mcp-server):npm installnpm run build✅ (tsc clean)npm test✅ (8 vitest tests: port-code & coordinate routing,passagesincludessuez,restrictions: ['suez','babelmandeb']yields a longer Suez-free route,speedKnots→durationHours,includeGeometry:falsedrops the GeoJSON, unknown-code →isError, andkdistinct alternatives sorted ascending)Also verified end-to-end over stdio with a real MCP client (
initialize→tools/list→tools/call): both tools list;sea_route('CNSHA','NLRTM')= 19,752.85 km via Suez;sea_route_alternativesreturns 3 sorted distinct routes (baseline 19,753 / no-malacca 20,759 / no-suez 25,315 km); an unknown code returnsisError: truewithUnknownPortError.Root package (unchanged behavior, confirmed still green from a clean tree):
npm ci·npm run lint✅ ·npm run format:check✅ ·npm run build✅ ·npm test✅ (67 tests).Closes #13
Generated by Claude Code