Skip to content

feat(mcp): a model argument for servers that serve several models, and basal-mcp --model - #66

Merged
rkinas merged 1 commit into
rkinas:mainfrom
pawelkiszczak:pr/mcp-model-argument
Nov 2, 2026
Merged

rkinas merged 1 commit into
rkinas:mainfrom
pawelkiszczak:pr/mcp-model-argument

Conversation

@pawelkiszczak

@pawelkiszczak pawelkiszczak commented Oct 28, 2026 •

Copy link
Copy Markdown
Contributor

Closes #65

What

  • Add optional model selection and list_models for MCP servers configured with a model-list callable.
  • Add basal-mcp --model NAME and forward the selected model in every request.
  • Document multi-model MCP servers and bridge usage.

Why

Allow MCP clients to route decisions to a chosen model on a multi-model server.

Compatibility

With models=None, tool names, schemas, instructions and request/answer behavior remain unchanged. basal-serve --mcp on continues to call http(srv, a.host) and remains single-model. No API-key behavior is included.

Tests run

  • uv run --no-sync pytest -q tests/test_mcp.py tests/test_server.py tests/test_http_status.py — 89 passed.
  • uv run --no-sync pytest -q -x — 765 passed, 53 skipped, 1 warning (expected hardware skips; one existing PyTorch flex-attention warning).
  • uvx [email protected] check --select E9,F63,F7,F82 --target-version py310 . — failed on existing, untouched basal/gemma4.py:471:66 (F821 Undefined name kpos).

Summary by Sourcery

Enable MCP clients and the basal-mcp bridge to route decisions to a selected model on multi-model servers.

New Features:

  • Add optional model selection and model discovery to MCP servers configured with multiple models.
  • Add a --model option to basal-mcp and forward the selected model with every request.

Enhancements:

  • Preserve the existing single-model MCP tool schemas and request behavior when no model list is configured.

Documentation:

  • Document multi-model MCP server configuration and stdio bridge usage.

Tests:

  • Add coverage for model listing, model selection, unknown-model errors, and bridge forwarding.

@sourcery-ai

sourcery-ai Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Reviewer's Guide

This PR adds opt-in multi-model support to MCP servers, exposing model discovery and per-request routing while maintaining backward-compatible single-model schemas and behavior. The stdio bridge gains --model forwarding, with documentation and coverage for tool schemas, selection, errors, and HTTP propagation.

Sequence diagram for MCP model discovery and selection

sequenceDiagram
    participant Client as MCP Client
    participant MCP as MCP Server
    participant Models as Model Registry
    participant Decision as Decision API

    Client->>MCP: list_models()
    MCP->>Models: models()
    Models-->>MCP: loaded model ids
    MCP-->>Client: models
    Client->>MCP: decide(model)
    MCP->>Decision: decide(body with model)
    Decision-->>MCP: answer with model
    MCP-->>Client: answer
Loading

Sequence diagram for basal-mcp model forwarding

sequenceDiagram
    participant Host as MCP Host
    participant Bridge as basal-mcp
    participant API as Basal HTTP API

    Host->>Bridge: decide(...)
    Bridge->>API: POST /v1/systemone with model
    API-->>Bridge: decision response
    Bridge-->>Host: MCP tool result
Loading

File-Level Changes

Change Details Files
Add optional multi-model routing to MCP tools while preserving the existing single-model interface.
  • Extend MCP server construction with a model-list callable and advertise multi-model behavior in server instructions.
  • Expose list_models and optional model parameters for decision tools when multiple models are configured.
  • Hide the model parameter and preserve existing tool schemas and request behavior when no model list is provided.
  • Forward selected model IDs in decision request bodies and propagate unknown-model errors.
basal/mcp_server.py
tests/test_mcp.py
Add model selection to the stdio bridge and document multi-model deployment patterns.
  • Add --model NAME to basal-mcp and include the selected model in every remote request.
  • Keep automatic model naming and unselected request behavior when the flag is omitted.
  • Document multi-model MCP servers, list_models, bridge configuration, and release notes.
basal/mcp_server.py
docs/MCP.md
docs/WHATS_NEW.md
tests/test_mcp.py

Assessment against linked issues

Issue Objective Addressed Explanation
#65 Allow MCP servers configured with a model-list callable to list loaded models and route decide and decide_many calls to an optional selected model. ✅
#65 Preserve existing MCP tool names, schemas, instructions, and request behavior when no model-list callable is configured. ✅
#65 Add basal-mcp --model NAME so the stdio bridge includes the selected model in every forwarded request. ✅

Possibly linked issues


Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="basal/mcp_server.py" line_range="109" />
<code_context>
         if lang:
             body["lang"] = lang
+        if model is not None:
+            body["model"] = model
         r = await ask(body)
         return {"model": r.get("model"), **r["answers"]["answer"]}
</code_context>
<issue_to_address>
**Selected model is ignored**

When `http()` is given a regular `Server` instance and a `models` callable, `Server.serve` passes the request to `Server.decide`, which never reads `body["model"]`, so the MCP tool returns a decision from the server's configured model instead of the selected one.

Have the HTTP adapter route the selected model to the corresponding server, or reject model selection when the supplied server cannot route requests.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

Comment thread basal/mcp_server.py
if lang:
body["lang"] = lang
if model is not None:
body["model"] = model

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Medium · Selected model is ignored

When http() is given a regular Server instance and a models callable, Server.serve passes the request to Server.decide, which never reads body["model"], so the MCP tool returns a decision from the server's configured model instead of the selected one.

Have the HTTP adapter route the selected model to the corresponding server, or reject model selection when the supplied server cannot route requests.

Prompt for AI agents
In `basal/mcp_server.py` at line 109:

**Selected model is ignored**

When `http()` is given a regular `Server` instance and a `models` callable, `Server.serve` passes the request to `Server.decide`, which never reads `body["model"]`, so the MCP tool returns a decision from the server's configured model instead of the selected one.

Have the HTTP adapter route the selected model to the corresponding server, or reject model selection when the supplied server cannot route requests.

@rkinas

rkinas commented Oct 28, 2026

Copy link
Copy Markdown
Owner

Thank you, Paweł! Planned for basal 2.5: we are merging this into the 2.5 release branch now, after our checks (full CPU suite; single-model MCP unchanged). The PR will close when 2.5 is published on main.

@rkinas
rkinas merged commit f811295 into rkinas:main Nov 2, 2026
1 check passed
rkinas added a commit that referenced this pull request Nov 2, 2026
… version

CONTRIBUTING.md credits the 2.5 pull requests as the release notes' community line lists them (Paweł Kiszczak
#52, #62, #64, #66, #68, #71, #73, #75; Marcin Brzeżański #56; Grzegorz Brzezinka #76, #78) and adds Dariusz
Koryto (#53, #58, #59, #60 and #55); the README's list names him too. docs/changes/README.md and the
changelog.py help use 2.5.1, the next release, as the example.
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.

MCP tools cannot select among multiple models served by one API server

2 participants