Skip to content

Add server instructions options to mcpcompat - #453

Open
Viphava280444 wants to merge 1 commit into
stacklok:mainfrom
Viphava280444:feat/mcpcompat-instructions
Open

Viphava280444 wants to merge 1 commit into
stacklok:mainfrom
Viphava280444:feat/mcpcompat-instructions

Conversation

@Viphava280444

@Viphava280444 Viphava280444 commented Nov 3, 2026 •

Copy link
Copy Markdown

Summary

  • Why: vMCP drops the instructions of backend servers toolhive#7020: vMCP drops the instructions text of its backend servers. To forward it, vMCP has to set the instructions field of its own initialize and server/discover results, and mcpcompat/server has no option for that field. go-sdk copies ServerOptions.Instructions into every session as is, while vMCP needs a different text per caller: only the backends that identity may reach.
  • What: two server options, following the shim's existing option pattern.
    • WithInstructions(text) sets a static text, like mcp-go's option of the same name. It is passed through to gosdk.ServerOptions.Instructions.
    • WithInstructionsFunc(func(ctx) string) computes the text per request from the request context, the one WithHTTPContextFunc fills. sessionDispatchMiddleware writes it onto the initialize and server/discover results after go-sdk built them, next to stripPublicCacheScope, which rewrites results for the same reason: go-sdk has no per-session hook for the field.
    • HandleMessage fills the field on its own initialize path.
    • An empty text omits the field, so servers that set neither option are unchanged.

The vMCP side is stacklok/toolhive#7047 (draft; its go.mod is pinned to this commit until this ships).

Type of change

  • Bug fix
  • New feature
  • Refactoring
  • Documentation
  • Other

Test plan

  • golangci-lint run ./... and go vet ./...: 0 issues
  • go test -race -count=1 ./... passes (./mcpcompat/server/... run 3 times)
  • New tests in mcpcompat/server/instructions_test.go: the default (no field), static text, per-request text (two callers get two texts), the stateless server, server/discover, and HandleMessage. They run in parallel like the rest of the package and post through a private transport, since httptest.Server.Close drops http.DefaultTransport's idle connections.
  • Manual: ToolHive vMCP built against this branch forwards the instructions of two FastMCP backends in initialize and server/discover, per caller.

The shim had no way to set the `instructions` field of the initialize
result, and go-sdk serves ServerOptions.Instructions verbatim to every
session. An aggregator such as vMCP needs a different text per caller
(only the backends the identity may use), so this adds two options:

- WithInstructions(text): static text, mirrors mcp-go's option.
- WithInstructionsFunc(func(ctx) string): computed per request from
  the request context (the one WithHTTPContextFunc fills). It is
  applied on the initialize and server/discover results in
  sessionDispatchMiddleware after go-sdk built them, like
  stripPublicCacheScope already rewrites results. When both are set
  the func wins. An empty result omits the field, so servers that set
  neither option are unchanged.

HandleMessage fills the field too. The initialize POST must stay
bridged (transports.go), since the func reads the identity there.

First half of stacklok/toolhive#7020.

Signed-off-by: Viphava280444 <[email protected]>
Viphava280444 added a commit to Viphava280444/toolhive that referenced this pull request Nov 3, 2026
The vMCP change needs server.WithInstructionsFunc, which is in
stacklok/toolhive-core#453 and not released yet. This replace lets CI
build against that commit on my fork.

Drop this commit and bump the toolhive-core version once the release
is out.

Signed-off-by: Viphava280444 <[email protected]>
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