Repository navigation
fix(mcp): refuse a missing project with a recovery hint (#1984) - #1990
Conversation
A call to a tool that requires `project` (bm_read, bm_write, bm_edit, bm_move, bm_delete, bm_list, bm_find, bm_context, bm_schema) without one returned pydantic's raw validation error. The model was not told what `project` is or how to find one. A client with a tool list cached from before `project` became required sends exactly this call. MissingProjectMiddleware checks the tool's published input schema. If `project` is required and neither `project` nor `project_id` is a non-blank string, it raises an INVALID_ARGUMENTS refusal that says "Try instead: bm_project(command='list') to discover projects". The refusal follows the call's output_format, else the tool's default. Reading the schema covers any tool on the server, including hosted replacements, without a list of names. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Drew Cain <groksrc@gmail.com>
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Three integration tests asserted pydantic's raw error for a missing or
blank `project` ("Missing required keyword only argument", "at least 1
character"). The server now answers those calls with the
INVALID_ARGUMENTS refusal, so they match that instead.
test_read_rejects_removed_pagination checks that bm_read rejects `page`.
It left out `project`, so the refusal answered first. It now passes
`project` and still checks for unexpected_keyword_argument.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Drew Cain <groksrc@gmail.com>
groksrc
left a comment
There was a problem hiding this comment.
No blocking findings. This change correctly turns a missing or blank project into an actionable Model Context Protocol tool error without weakening the published required schema.
I reviewed head commit 139b36e814bcbc72bc09bb67baccf93242ac897b. MissingProjectMiddleware reads each tool's current input schema and intercepts only tools that require project. It accepts a nonblank project or project_id, and it preserves the requested or schema-default text/JSON output format. Registering it on the shared Basic Memory server also covers hosted tools added to that server instance.
Verification:
BASIC_MEMORY_ENV=test uv run pytest -q --no-cov tests/mcp/test_missing_project.pypassed:54 passed in 6.46s.git diff --check origin/main...origin/shipright/BMEM-68returned no errors.- GitHub's Tests, CodeQL, DCO, and pull-request title checks all pass at this revision.
The tests cover all nine one-project tools, blank values, JSON refusals, the bm_context JSON default, the project_id alias, a non-core required-project tool, optional-project tools, and the shipped server registration.
Next step: a repository maintainer can merge this pull request through the normal approval process.
Summary
A call that leaves out
projecton a tool that requires it now gets a Basic Memory refusal instead of pydantic's raw validation error. The refusal tells the model to callbm_project(command='list')to find a project. Fixes #1984.Before:
After (text):
Why
PR #1934 made
projectrequired on the nine one-project tools: bm_read, bm_write, bm_edit, bm_move, bm_delete, bm_list, bm_find, bm_context and bm_schema. That is intended. But a client holding a tool list cached from before #1934 still treatsprojectas optional. Its calls failed with an error that does not say whatprojectis or how to find one.What changed
MissingProjectMiddlewareinsrc/basic_memory/mcp/missing_project.py. On each tool call it looks up the tool and reads its input schema.projectis not in the schema'srequiredlist, the call passes through untouched. bm_search and bm_project are in this group.projectnor its aliasproject_idis a non-blank string, it raises anINVALID_ARGUMENTSrefusal through the existingrefusal()helper (src/basic_memory/mcp/refusals.py).output_formatwhen it is "text" or "json", else the tool's schema default. bm_context defaults to JSON, so its refusal is JSON.src/basic_memory/mcp/server.pyregisters the middleware after the two existing ones.The check reads the schema, not a list of tool names. So it also covers tools that replace or add to the core tools on the same server instance, such as a hosted deployment's own tools.
Rejected alternatives:
projectdefault toNoneand checking it in each tool. That publishesprojectas optional again, which undoes fix(mcp): require explicit project scope for note tools #1934.ValidationErrorafter the call. That depends on how FastMCP wraps it, which can change between releases.Tests
New
tests/mcp/test_missing_project.py(54 tests, in-memory FastMCP client):projectmissing,None,""and" ", withisErrorset and the hint text.output_format="json"gives{"error": "INVALID_ARGUMENTS", ..., "try_instead": ...}. bm_context with nooutput_formatgives JSON too.project: ProjectParamis refused the same way, which shows the check reads the schema.projectorproject_idset passes through. A tool with an optionalprojectis untouched. bm_search keeps its own scope refusal. Unknown tools still get FastMCP's error.mcpinstance has the middleware and refusesbm_listwith no project.Local runs on this branch:
BASIC_MEMORY_ENV=test uv run pytest -q --no-cov -n 8 --timeout 60 tests/mcp:1234 passed, 4 warnings in 976.43s.ruff check,ruff format --check,ty checkandpyrighton the changed files: clean.Updated integration tests (second commit,
139b36e8):test-int/mcp/test_default_project_mode_integration.py(2 tests) andtest-int/mcp/test_single_project_mcp_integration.pyasserted pydantic's old text (Missing required keyword only argument,at least 1 character). They now matchINVALID_ARGUMENTS: bm_write needs \project``.test-int/mcp/test_param_aliases_integration.py::test_read_rejects_removed_paginationchecks that bm_read rejectspage. It left outproject, so the new refusal answered first. It now passesprojectand still assertsunexpected_keyword_argument.Rollback
Remove the
mcp.add_middleware(MissingProjectMiddleware())line inserver.py.Tracking: BMEM-68.
🤖 Generated with Claude Code