Skip to content

fix(mcp): refuse a missing project with a recovery hint (#1984) - #1990

Merged
groksrc merged 2 commits into
mainfrom
shipright/BMEM-68
Oct 30, 2026
Merged

groksrc merged 2 commits into
mainfrom
shipright/BMEM-68

Conversation

@groksrc

@groksrc groksrc commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

Summary

A call that leaves out project on a tool that requires it now gets a Basic Memory refusal instead of pydantic's raw validation error. The refusal tells the model to call bm_project(command='list') to find a project. Fixes #1984.

Before:

Missing required keyword only argument [type=missing_keyword_only_argument ...]

After (text):

INVALID_ARGUMENTS: bm_list needs `project`: the project name, 'workspace/project', or external id.
Try instead: bm_project(command='list') to discover projects

Why

PR #1934 made project required 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 treats project as optional. Its calls failed with an error that does not say what project is or how to find one.

What changed

  • New MissingProjectMiddleware in src/basic_memory/mcp/missing_project.py. On each tool call it looks up the tool and reads its input schema.
    • If project is not in the schema's required list, the call passes through untouched. bm_search and bm_project are in this group.
    • Otherwise, if neither project nor its alias project_id is a non-blank string, it raises an INVALID_ARGUMENTS refusal through the existing refusal() helper (src/basic_memory/mcp/refusals.py).
    • The refusal uses the call's output_format when 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.py registers 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:

Tests

New tests/mcp/test_missing_project.py (54 tests, in-memory FastMCP client):

  • Each of the nine tools refuses project missing, None, "" and " ", with isError set and the hint text.
  • output_format="json" gives {"error": "INVALID_ARGUMENTS", ..., "try_instead": ...}. bm_context with no output_format gives JSON too.
  • A test-only tool with project: ProjectParam is refused the same way, which shows the check reads the schema.
  • project or project_id set passes through. A tool with an optional project is untouched. bm_search keeps its own scope refusal. Unknown tools still get FastMCP's error.
  • The shipped mcp instance has the middleware and refuses bm_list with 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 check and pyright on the changed files: clean.

Updated integration tests (second commit, 139b36e8):

  • test-int/mcp/test_default_project_mode_integration.py (2 tests) and test-int/mcp/test_single_project_mcp_integration.py asserted pydantic's old text (Missing required keyword only argument, at least 1 character). They now match INVALID_ARGUMENTS: bm_write needs \project``.
  • test-int/mcp/test_param_aliases_integration.py::test_read_rejects_removed_pagination checks that bm_read rejects page. It left out project, so the new refusal answered first. It now passes project and still asserts unexpected_keyword_argument.

Rollback

Remove the mcp.add_middleware(MissingProjectMiddleware()) line in server.py.

Tracking: BMEM-68.

🤖 Generated with Claude Code

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>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-30T03:57:20.307960Z 3f5ad9c PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

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 groksrc left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

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.py passed: 54 passed in 6.46s.
  • git diff --check origin/main...origin/shipright/BMEM-68 returned 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.

@groksrc groksrc added the review:done Automated review found no blocking issues label Oct 30, 2026
@groksrc
groksrc merged commit b1e3744 into main Oct 30, 2026
45 checks passed
@groksrc
groksrc deleted the shipright/BMEM-68 branch October 30, 2026 04:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review:done Automated review found no blocking issues

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Missing project on one-project tools returns a raw pydantic error instead of a refusal

1 participant