Skip to content

feat(mcp): restrict servers to specific models via models field - #2487

Closed
kevingatera wants to merge 8 commits into
MoonshotAI:mainfrom
kevingatera:feat/mcp-model-scoping
Closed

feat(mcp): restrict servers to specific models via models field#2487
kevingatera wants to merge 8 commits into
MoonshotAI:mainfrom
kevingatera:feat/mcp-model-scoping

Conversation

@kevingatera

@kevingatera kevingatera commented Jul 31, 2026

Copy link
Copy Markdown

Related Issue

Resolve #2480

Problem

All configured MCP servers are loaded for every model. Users who mix models (e.g. a strong primary model and a cheaper or specialized secondary one) cannot keep tool-heavy or provider-specific MCP servers away from models that should not use them.

What changed

  • Add an optional models field to MCP server configuration (stdio, http, and sse transports): exact model aliases plus trailing-* wildcards; servers with models are skipped for non-matching models, and restricted servers are excluded when the model is unknown.
  • Filter is applied when creating a session (using the session's model) and when resuming (using the session's persisted model, recovered from its records; falls back to the configured default when none is persisted).
  • Backward compatible: servers without models load for every model as before.
  • Tests cover the matching semantics and the session-creation wiring; docs updated in both locales.

Notes for reviewers

  • Scoping is evaluated once at session start: switching models mid-session does not reload or re-filter MCP servers.
  • The v2 engine (packages/agent-core-v2) has its own MCP config stack and does not yet honor models; a follow-up can add it if the approach is accepted.
  • On resume, filtering uses config.defaultModel, not the model the session was created with.

Checklist

  • I have read the CONTRIBUTING document.
  • I have linked a related issue, or explained the problem above.
  • I have added tests that prove my feature works.
  • Ran gen-changesets skill, or this PR needs no changeset.
  • Ran gen-docs skill, or this PR needs no doc update.

MCP servers were loaded for every session regardless of model, so a
model-specific MCP (e.g. the z.ai vision server) could not be attached to
just one model without leaking its tools to others. Add an optional models
field to the MCP server config (exact alias key or trailing-* prefix
wildcard) and filter the merged server set by the session's model alias at
create/resume time. Servers without the field keep loading everywhere.
@changeset-bot

changeset-bot Bot commented Jul 31, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: da283e4

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@moonshot-ai/kimi-code Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9b7544737e

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

const withCallerMcp = mergeCallerMcpServers(baseMcpConfig, input.mcpServers);
// Resume carries no model override in its payload; use the configured
// default to decide which model-scoped MCPs load.
const modelAlias = config.defaultModel;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Use the persisted model when filtering resumed MCPs

When a session was created with an explicit model—or switched models before closing—and that alias differs from the current global default, this filters its MCP servers against the unrelated default before session.resume() replays the persisted config.update records. The resumed agent therefore keeps its persisted model while receiving the wrong MCP set: matching servers disappear and servers scoped to the default model may connect. Recover the persisted main-agent alias before filtering, or defer filtering until its configuration has been replayed.

Useful? React with 👍 / 👎.

…odel

Resume filtered model-scoped MCP servers against config.defaultModel, but a
session may have been created with an explicit --model or switched models
mid-session; that alias is persisted on the wire and replayed by
session.resume() after the filter ran. Recover the last config.update model
alias from the main agent's wire.jsonl before filtering, falling back to the
configured default when no alias is persisted.
@liruifengv

Copy link
Copy Markdown
Collaborator

Thank you for your interest in contributing to Kimi Code.

For new features, please create an issue or discuss it in an existing issue. We are not currently accepting pull requests for new features.

@liruifengv liruifengv closed this Aug 1, 2026
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.

Feature request: restrict MCP servers to specific models via a models field

2 participants