Skip to content

feat(wren): expand MCP knowledge access - #2439

Merged
goldmedal merged 2 commits into
feat/serve-mcpfrom
feat/serve-mcp-knowledge
Jul 5, 2026
Merged

goldmedal merged 2 commits into
feat/serve-mcpfrom
feat/serve-mcp-knowledge

Conversation

@goldmedal

@goldmedal goldmedal commented Jul 5, 2026 •

Copy link
Copy Markdown
Collaborator

Follow-up to #2438 (based on feat/serve-mcp; retarget to main once #2438 merges).

What

Broadens what wren serve mcp exposes as knowledge — previously just get_instructions (business rules) and recall_queries (NL→SQL pairs). Adds semantic schema retrieval, full schema description, stored-query listing, and generic access to any file under knowledge/.

New tools (all read-only)

Tool Purpose
get_context(question, …) Semantic schema retrieval — the schema-axis twin of recall_queries. Uses the memory index for large schemas; falls back to full describe_schema text (+ a hint to pip install 'wrenai[memory]' and wren memory index) when the extra/index is absent.
describe_schema() Full schema as human-readable plaintext (the readable twin of get_mdl's JSON).
list_stored_queries(source?, limit?) Enumerate all stored NL→SQL pairs (via MemoryStore, or a knowledge/sql/*.md fallback).
list_knowledge() List files under knowledge/ (+ whether AGENTS.md exists) so a client knows what to read.

New resources

  • wren://agents — the project's AGENTS.md
  • wren://knowledge/{path} — serve any file under knowledge/ (rules, sql, knowledge.yml)
  • wren://project enriched with knowledge_schema_version

Security

wren://knowledge/{path} has a directory-traversal guard: the resolved target must satisfy is_relative_to(<project>/knowledge) and is_file(), else it raises. Verified server-side with raw, un-normalized URIs (not relying on client URL normalization) — wren://knowledge/../wren_project.yml is rejected.

Note on FastMCP templates

mcp[cli] URI-template placeholders are single-segment ([^/]+), so a single {path} can't span /. Implemented as two templates (wren://knowledge/{name} + wren://knowledge/{subdir}/{name}), covering the one-level knowledge/ layout (rules/, sql/, …).

Testing

  • ruff format/ruff check clean
  • End-to-end stdio smoke test against a DuckDB project exercising the fallback (no-memory-extra) paths: 9/9 checks pass, including the traversal-rejection assertion. get_context correctly returns strategy="full" + the install hint; list_stored_queries/list_knowledge read from knowledge/; wren://agents and wren://knowledge/* resources resolve.

Summary by CodeRabbit

  • New Features

    • Added more knowledge browsing options, including schema context lookup, schema descriptions, stored query listings, and project knowledge file discovery.
    • Added new resources for accessing agent notes and knowledge files directly from the server.
  • Bug Fixes

    • Improved fallback behavior when optional knowledge features are unavailable, so schema and query information still remains accessible.
    • Added safer handling for knowledge file access, including invalid text and path-escape protection.
  • Documentation

    • Updated MCP and CLI docs to cover the expanded tools, resources, and fallback behavior.

Broaden what `wren serve mcp` exposes beyond rules + NL→SQL recall.

- get_context: semantic schema retrieval (schema-axis twin of
  recall_queries; falls back to full describe_schema text when the
  memory extra / index is absent)
- describe_schema: full plaintext schema (human-readable get_mdl)
- list_stored_queries: enumerate all stored NL→SQL pairs (MemoryStore
  or knowledge/sql/*.md fallback)
- list_knowledge: list files under knowledge/ (+ AGENTS.md presence)
- resource wren://agents (project AGENTS.md)
- resource template wren://knowledge/{path} with a directory-traversal
  guard (resolve + is_relative_to + is_file)
- enrich wren://project with knowledge_schema_version

FastMCP URI-template placeholders are single-segment, so knowledge
files are served via two templates covering the one-level layout.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@coderabbitai

coderabbitai Bot commented Jul 5, 2026 •

Copy link
Copy Markdown
Contributor

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 2acda34d-9f50-4b94-9171-f6bb8ad75661

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • ✅ Review completed - (🔄 Check again to review again)
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch feat/serve-mcp-knowledge

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot added documentation Improvements or additions to documentation python Pull requests that update Python code core labels Jul 5, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Nitpick comments (1)
core/wren/src/wren/mcp_server.py (1)

545-576: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Consider a wildcard knowledge resource template. mcp 1.28.1 supports {path*} in FastMCP.resource(), so a single wren://knowledge/{path*} route can replace the two fixed-depth templates and better match the nested knowledge layout.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@core/wren/src/wren/mcp_server.py` around lines 545 - 576, The knowledge
resource routing is split into two fixed-depth handlers even though the MCP SDK
supports a wildcard path segment. Update the resource registration around
knowledge_root_resource and knowledge_nested_resource to use a single
wren://knowledge/{path*} template, and adjust _read_knowledge_file to accept the
full relative path so nested knowledge files are handled consistently while
still rejecting escapes.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@core/wren/src/wren/mcp_server.py`:
- Around line 539-543: The `agents_resource()` and `_read_knowledge_file()`
paths currently read files with `read_text(encoding="utf-8")` without handling
decode failures, so a non-UTF-8 `AGENTS.md` or binary file under `knowledge/`
will surface as an unhandled `UnicodeDecodeError`. Add explicit
`UnicodeDecodeError` handling around the UTF-8 reads in these helpers and
re-raise a clear `ValueError` with the file path/context, keeping the existing
`mcp.resource` and `_read_knowledge_file` behavior otherwise unchanged.
- Around line 447-465: list_knowledge() currently advertises every file found by
rglob("*"), including paths deeper than wren://knowledge/{path} can actually
serve. Update list_knowledge() to enforce the supported knowledge depth by
filtering the files it returns to only top-level knowledge files and one
subdirectory level, or otherwise include a depth check in the payload so only
readable paths are listed. Use the list_knowledge() function and its
knowledge_dir traversal logic as the place to apply the constraint.
- Around line 360-384: The fallback in the MCP server context lookup is catching
all exceptions, which hides real failures behind the generic memory-install
message. Update the logic around MemoryStore creation and get_context() in the
relevant mcp_server method to only catch dependency/import-related errors,
matching the ImportError-only behavior used by recall_queries, and allow other
runtime failures to propagate instead of returning the fallback response.

---

Nitpick comments:
In `@core/wren/src/wren/mcp_server.py`:
- Around line 545-576: The knowledge resource routing is split into two
fixed-depth handlers even though the MCP SDK supports a wildcard path segment.
Update the resource registration around knowledge_root_resource and
knowledge_nested_resource to use a single wren://knowledge/{path*} template, and
adjust _read_knowledge_file to accept the full relative path so nested knowledge
files are handled consistently while still rejecting escapes.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: 574b0b9c-2186-465e-9202-0dbe3ddb682f

📥 Commits

Reviewing files that changed from the base of the PR and between a585501 and 23cb233.

📒 Files selected for processing (3)
  • core/wren/src/wren/mcp_server.py
  • docs/core/guides/mcp.md
  • docs/core/reference/cli.md

Comment thread core/wren/src/wren/mcp_server.py Resolved
Comment thread core/wren/src/wren/mcp_server.py Resolved
Comment thread core/wren/src/wren/mcp_server.py Resolved
- get_context: narrow the full-text fallback to ImportError (missing
  memory extra) so real index/embedding errors surface instead of being
  masked as "install wrenai[memory]"
- list_knowledge: list only files the wren://knowledge/{path} templates
  can serve (top level + one subdir), not arbitrarily deep paths
- knowledge/AGENTS.md resources: raise a clear ValueError on non-UTF-8
  files instead of an unhandled UnicodeDecodeError

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
@goldmedal
goldmedal merged commit 9af78e2 into feat/serve-mcp Jul 5, 2026
9 checks passed
@goldmedal
goldmedal deleted the feat/serve-mcp-knowledge branch July 5, 2026 15:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core documentation Improvements or additions to documentation python Pull requests that update Python code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant