Skip to content

fix: stop the assistant inventing filters, and bound MCP discovery - #367

Merged
neuromechanist merged 1 commit into
developfrom
fix/mcp-release-review
Sep 16, 2026
Merged

neuromechanist merged 1 commit into
developfrom
fix/mcp-release-review

Conversation

@neuromechanist

Copy link
Copy Markdown
Member

Addresses the review findings on the release pull request #366. Three independent reviewers plus my own pass; this covers the correctness bugs, the blocked gate, and the availability risk. The remainder is filed as follow-ups.

The one that produces wrong science

The system prompt's own worked example told the model to call the primary tool with a parameter that does not exist. search_datasets takes modality; the prompt said modality_filter. The server's input schema carries "additionalProperties": {}, so it accepts the unknown key and silently drops it.

Verified against the live server:

search_datasets(query="attention", modality_filter="MEG", limit=3)  ->  count: 92, isError unset
search_datasets(query="attention", modality="MEG",        limit=3)  ->  count: 3

A researcher asking for MEG datasets gets 92 mostly-EEG results presented as filtered, with nothing in the response or the logs marking it. The prompt also advertised a participant-count filter that does not exist (same silent-drop mechanism), and two of the four canned widget questions were written for the old toolset: one asks for "MEG datasets with at least 50 participants", the query this toolset is least able to answer, and one names ds000248, an OpenNeuro accession the rewritten prompt itself says is not a valid identifier.

Fixed by naming the six real filters explicitly, saying in the prompt that unknown arguments are silently ignored, and pointing participant questions at subject_count on the results. New canned values checked live: modality accepts either case (85 results either way) and nm000219 describes cleanly.

The one that takes the whole API down

discover_mcp_tools ends in pool.submit(...).result() with no timeout, and create_community_assistant is uncached and called per request directly from async def handlers, so that synchronous block runs on the event loop thread. Meanwhile asyncio.wait_for wrapped only list_tools(); the connect and protocol handshake sat outside it, bounded solely by the SDK's 300 second read default.

So a NEMAR host that accepts the socket and then stalls froze every community's requests for up to five minutes, failed the health probe, and restarted the container. Two reviewers reproduced it independently; one held a socket open and watched discovery still hanging at 75 seconds.

Fixed by putting connect and handshake inside the timeout in both _discover and _call, giving .result() a bound, and caching discovered tools for five minutes so the round trip leaves the request hot path entirely.

One trap worth naming: with ThreadPoolExecutor(...) exits via shutdown(wait=True), so adding a timeout to .result() alone still blocks on the hung worker at the with exit. The executor is now constructed without the context manager and shut down with wait=False, cancel_futures=True.

The one that misleads a researcher

When discovery returns nothing, the prompt still described six tools that were not bound, and separately instructs the model to answer rather than decline. The likely output is confident dataset accessions with real-looking links and invented citations. _load_mcp_tools now reports which servers yielded nothing, and the constructor appends an instruction not to answer from memory or invent an identifier, and to point the user at https://nemar.org/discover.

_load_mcp_tools returns (tools, degraded) rather than mutating instance state, so it stays callable as an unbound function, which its tests rely on.

Also

  • A tool call that timed out or hit a transport error used to end the turn. langgraph's default ToolNode handler converts only ToolInvocationError and re-raises the rest, and these tools carry a raw JSON Schema rather than a pydantic model, so that error never occurs for them. Failures now reach the model as readable text, the way a server refusal already did.
  • One malformed tool descriptor used to discard every tool from that server, and the log blamed the network. Tools are wrapped one at a time now, and the catch-all no longer asserts a cause.
  • A paginated tool list was silently truncated; it now logs.
  • mcp is capped below 3.0.0. The production image installs from pyproject.toml, not uv.lock, so an unreviewed major would otherwise reach production on the next build.
  • test.yml's lint step ran ruff format --check ., which reaches Python snippets inside seven Markdown docs and fails on all of them. That is why All Tests Passed is red on Release 0.8.9: NEMAR assistant on the MCP server #366 today. Scope narrowed to src tests, matching tests.yml.

Test

Two mutation checks, because a test that cannot fail is worse than no test.

  • The new stalling-socket test binds a listener that accepts and never answers. Against the pre-fix mcp_client.py it hung until killed at 75 seconds (timeout exit 124). With the fix it passes in 2.29 seconds.
  • test_a_refusal_comes_back_as_text_not_an_exception asserted only a substring that the fallback path also produces, so deleting the is_error branch left it green. It now asserts the distinguishing prefix, and deleting that branch fails it.

test_every_prompt_named_tool_actually_exists contained assert named <= available where named was built by filtering available, so it could never fail. It now compares an explicit literal set in both directions: tools the prompt names that the server does not serve, and tools the server serves that the prompt never names.

Full offline suite: 1872 passed, 32 skipped. ruff check and ruff format --check clean.

Follow-ups, not in this pull request

The drift tests that would catch NEMAR renaming a tool are @pytest.mark.network, deselected by both workflows on every pull request, and the post-merge job that runs them is continue-on-error: true, so they can never fail a build. .context/nemar-api.md still documents the deleted endpoint as live. CLAUDE.md claims a 70 percent coverage minimum that nothing enforces. Two workflows are both named Tests and both match every pull request. nemar_render_overview returns a PNG that _text_of drops and the widget does not render.

@neuromechanist
neuromechanist merged commit d357f89 into develop Sep 16, 2026
7 checks passed
@neuromechanist
neuromechanist deleted the fix/mcp-release-review branch September 16, 2026 23:33
neuromechanist added a commit that referenced this pull request Sep 30, 2026
The file had sections for 0.8.10, 0.8.12, 0.8.14 and 0.8.15 only. The
three missing releases are written from the pull requests between their
tags (0.8.13: #438, #440, #441, #443; 0.8.11: #418; 0.8.9: #351 to #355
and #359 and #367), so the last seven releases are now all present.

Tested: docs only; each entry checked against its pull request's body.
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