Repository navigation
feat(wren): expand MCP knowledge access - #2439
Conversation
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]>
|
Important Review skippedAuto reviews are disabled on base/target branches other than the default branch. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
✨ Finishing Touches🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 3
🧹 Nitpick comments (1)
core/wren/src/wren/mcp_server.py (1)
545-576: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick winConsider a wildcard knowledge resource template.
mcp1.28.1 supports{path*}inFastMCP.resource(), so a singlewren://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
📒 Files selected for processing (3)
core/wren/src/wren/mcp_server.pydocs/core/guides/mcp.mddocs/core/reference/cli.md
- 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]>
Follow-up to #2438 (based on
feat/serve-mcp; retarget tomainonce #2438 merges).What
Broadens what
wren serve mcpexposes as knowledge — previously justget_instructions(business rules) andrecall_queries(NL→SQL pairs). Adds semantic schema retrieval, full schema description, stored-query listing, and generic access to any file underknowledge/.New tools (all read-only)
get_context(question, …)recall_queries. Uses the memory index for large schemas; falls back to fulldescribe_schematext (+ a hint topip install 'wrenai[memory]'andwren memory index) when the extra/index is absent.describe_schema()get_mdl's JSON).list_stored_queries(source?, limit?)MemoryStore, or aknowledge/sql/*.mdfallback).list_knowledge()knowledge/(+ whetherAGENTS.mdexists) so a client knows what to read.New resources
wren://agents— the project'sAGENTS.mdwren://knowledge/{path}— serve any file underknowledge/(rules, sql, knowledge.yml)wren://projectenriched withknowledge_schema_versionSecurity
wren://knowledge/{path}has a directory-traversal guard: the resolved target must satisfyis_relative_to(<project>/knowledge)andis_file(), else it raises. Verified server-side with raw, un-normalized URIs (not relying on client URL normalization) —wren://knowledge/../wren_project.ymlis 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-levelknowledge/layout (rules/,sql/, …).Testing
ruff format/ruff checkcleanmemory-extra) paths: 9/9 checks pass, including the traversal-rejection assertion.get_contextcorrectly returnsstrategy="full"+ the install hint;list_stored_queries/list_knowledgeread fromknowledge/;wren://agentsandwren://knowledge/*resources resolve.Summary by CodeRabbit
New Features
Bug Fixes
Documentation