Skip to content

feat: add discover-spaces tool and MCP_INSTRUCTIONS support#2

Draft
milichev wants to merge 3 commits into
feature/http-mcp-transportfrom
feature/space-discovery-tool
Draft

feat: add discover-spaces tool and MCP_INSTRUCTIONS support#2
milichev wants to merge 3 commits into
feature/http-mcp-transportfrom
feature/space-discovery-tool

Conversation

@milichev
Copy link
Copy Markdown
Owner

@milichev milichev commented Mar 13, 2026

Warning

This PR is not supposed to be merged.
Instead, it will be re-targeted to anyproto/anytype-mcp:main once the outstanding PR anyproto#59 is merged.
Therefore, it's marked as Draft. Still, you can review and try it out.

  • I understand that contributing to this repository will require me to agree with the CLA

Description

This PR adds three related improvements:

discover-spaces tool — a hand-crafted MCP tool that fetches all Anytype spaces with their types, properties, tags, and select option IDs in a single call. Reduces context bloat and roundtrips: clients call this once per session to resolve all IDs, instead of chaining list-spaces → list-types → list-properties → list-tags. Supports bracket-notation path narrowing (spaces["My Space"].tags), TTL cache with force_refresh, and per-space/type opt-in filtering via DISCOVERY_TOOL_CONFIG. The tool significantly decreases MCP-LLM roundtrips:

Claude Chat

MCP_INSTRUCTIONS — wires instructions.md into ServerOptions.instructions so MCP clients receive usage guidance on connect. The file is inlined at build time via esbuild define, so no runtime FS dependency in the distributed CLI. Configurable: false disables, any other string overrides with custom content.

Config refactor — centralises all environment variable parsing into a single Zod-validated ConfigSchema. Adds a JsonString<T> helper that labels parse errors with the originating env var name. Adds a {file:/path/to/file} mini-DSL, inspired by OpenCode, for DISCOVERY_TOOL_CONFIG and MCP_INSTRUCTIONS, so complex configs can be loaded from a file instead of inlined as escaped JSON strings. Validates all new env vars at startup alongside existing ones.

image

What type of PR is this? (check all applicable)

  • 🍕 Feature
  • 🧑‍💻 Code Refactor
  • ✅ Test
  • 🤖 Build

Related Tickets & Documents

  • instructions.md — usage guide broadcast to MCP clients on connect

Mobile & Desktop Screenshots/Recordings

N/A

Added tests?

  • 👍 yes

New/updated test files:

  • src/utils/__tests__/config.test.ts (renamed from proxy-config.test.ts; extended with DISCOVERY_TOOL_CONFIG, MCP_INSTRUCTIONS, JsonString, and {file:} coverage)
  • src/utils/__tests__/getPlainAxios.test.ts (isolated from axios mock interference)
  • src/mcp/tools/__tests__/discovery.test.ts (resolvePath, fetchDiscovery, makeDiscoverSpacesHandler including cache, TTL, and force_refresh)

Added to documentation?

  • 📜 README.md

Added DISCOVERY_TOOL_CONFIG and MCP_INSTRUCTIONS to the environment variables table; added discover-spaces tool section covering path narrowing, space/type filtering, {file:} config reference, and cache behaviour.

[optional] Are there any post-deployment tasks we need to perform?

None. instructions.md is bundled into bin/cli.mjs at build time — no deploy-time file placement needed.

@milichev milichev changed the title Feature/space discovery tool feat: add discover-spaces tool and MCP_INSTRUCTIONS support Mar 13, 2026
@milichev milichev self-assigned this Mar 13, 2026
@milichev milichev force-pushed the feature/http-mcp-transport branch from 5b0ac31 to 23ef956 Compare March 14, 2026 17:20
@milichev milichev force-pushed the feature/space-discovery-tool branch from a88f8f8 to 7d36e5a Compare March 14, 2026 17:41
@milichev milichev force-pushed the feature/http-mcp-transport branch from 23ef956 to 003d296 Compare March 31, 2026 16:31
@milichev milichev force-pushed the feature/space-discovery-tool branch from bc07157 to ace001b Compare March 31, 2026 16:31
- Add discover-spaces hand-crafted tool: fetches all spaces with their
  types, properties, tags, and select option IDs; bracket-notation path
  narrowing; TTL cache with force_refresh; per-space/type opt-in filter
  via DISCOVERY_TOOL_CONFIG

- Centralise env parsing into Zod-validated ConfigSchema; add JsonString
  helper with envKey labelling for error attribution; HttpClientConfig,
  ToolsConfig, InstructionsSchema all validated at startup

- Add MCP_INSTRUCTIONS env var: unset/"true" → bundled instructions.md
  (inlined by esbuild via __BUNDLED_INSTRUCTIONS__); "false" → disabled;
  any other string → custom content; wired into serverOptions.instructions

- Extract getPlainAxios singleton (non-OpenAPI-aware Axios) to
  src/utils/getPlainAxios.ts; extract resolveInstructions to
  src/utils/resolveInstructions.ts

- Rename proxy-config.test.ts → config.test.ts; add test coverage for
  DISCOVERY_TOOL_CONFIG, MCP_INSTRUCTIONS, JsonString, and getPlainAxios;
  split getPlainAxios tests into dedicated file to isolate from axios mock

- env:OPENAPI_MCP_HEADERS error message fix; discovery tool annotations
…PI_MCP_HEADERS='{file:headers.json}'; discover-spaces tool documentation added
@milichev milichev force-pushed the feature/http-mcp-transport branch from 003d296 to 9736b33 Compare March 31, 2026 17:08
@milichev milichev force-pushed the feature/space-discovery-tool branch from ace001b to 6155870 Compare March 31, 2026 17:09
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