Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion pkg/github/comment_minimize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ func TestCommentVisibilityTypedInputSchemaMatchesLegacy(t *testing.T) {
}

func connectCommentVisibilityClient(t *testing.T, server *mcp.Server, version string) *mcp.ClientSession {
t.Helper()
return connectCommentVisibilityClientWithCapabilities(t, server, version, nil)
}

func connectCommentVisibilityClientWithCapabilities(t *testing.T, server *mcp.Server, version string, caps *mcp.ClientCapabilities) *mcp.ClientSession {
t.Helper()
if version == "2025-11-25" {
server.AddReceivingMiddleware(func(next mcp.MethodHandler) mcp.MethodHandler {
Expand All @@ -234,7 +239,8 @@ func connectCommentVisibilityClient(t *testing.T, server *mcp.Server, version st
serverSession, err := server.Connect(context.Background(), serverTransport, nil)
require.NoError(t, err)
t.Cleanup(func() { _ = serverSession.Close() })
client := mcp.NewClient(&mcp.Implementation{Name: "comment-visibility-test", Version: "v1"}, nil)
clientOpts := &mcp.ClientOptions{Capabilities: caps}
client := mcp.NewClient(&mcp.Implementation{Name: "comment-visibility-test", Version: "v1"}, clientOpts)
session, err := client.Connect(context.Background(), commentVisibilityProtocolTransport{clientTransport, version}, nil)
require.NoError(t, err)
t.Cleanup(func() { _ = session.Close() })
Expand Down
18 changes: 13 additions & 5 deletions pkg/github/encoded_schemas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,16 @@ type schemaListWire struct {
Error json.RawMessage `json:"error"`
}

func schemaListPages(ctx context.Context, t *testing.T, inv *inventory.Inventory, optimized bool, transport, protocol string, pageSize int, schemaCache *mcp.SchemaCache) ([][]byte, []string) {
func schemaListClientCapabilitiesJSON(ui bool) string {
if ui {
return `{"extensions":{"io.modelcontextprotocol/ui":{"mimeTypes":["text/html;profile=mcp-app"]}}}`
}
return `{}`
}

func schemaListPages(ctx context.Context, t *testing.T, inv *inventory.Inventory, optimized bool, transport, protocol string, pageSize int, schemaCache *mcp.SchemaCache, ui bool) ([][]byte, []string) {
t.Helper()
capsJSON := schemaListClientCapabilitiesJSON(ui)
var request func(string) []byte
switch transport {
case "stdio":
Expand All @@ -68,7 +76,7 @@ func schemaListPages(ctx context.Context, t *testing.T, inv *inventory.Inventory
require.NoError(t, err)
return response
}
init := exchange(fmt.Sprintf(`{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":%q,"capabilities":{},"clientInfo":{"name":"test","version":"1"}}}`, protocol))
init := exchange(fmt.Sprintf(`{"jsonrpc":"2.0","id":1,"method":"initialize","params":{"protocolVersion":%q,"capabilities":%s,"clientInfo":{"name":"test","version":"1"}}}`, protocol, capsJSON))
require.NotContains(t, string(init), `"error"`)
_, err = io.WriteString(clientConn, `{"jsonrpc":"2.0","method":"notifications/initialized"}`+"\n")
require.NoError(t, err)
Expand Down Expand Up @@ -97,7 +105,7 @@ func schemaListPages(ctx context.Context, t *testing.T, inv *inventory.Inventory
var pages [][]byte
names := make([]string, 0)
listRequest := func(id int, cursor string) string {
return fmt.Sprintf(`{"jsonrpc":"2.0","id":%d,"method":"tools/list","params":{"cursor":%q,"_meta":{"io.modelcontextprotocol/protocolVersion":%q,"io.modelcontextprotocol/clientInfo":{"name":"test","version":"1"},"io.modelcontextprotocol/clientCapabilities":{}}}}`, id, cursor, protocol)
return fmt.Sprintf(`{"jsonrpc":"2.0","id":%d,"method":"tools/list","params":{"cursor":%q,"_meta":{"io.modelcontextprotocol/protocolVersion":%q,"io.modelcontextprotocol/clientInfo":{"name":"test","version":"1"},"io.modelcontextprotocol/clientCapabilities":%s}}}`, id, cursor, protocol, capsJSON)
}
cursor := ""
for {
Expand Down Expand Up @@ -195,8 +203,8 @@ func TestEncodedSchemasWireParity(t *testing.T) {
wantNames = slices.Compact(wantNames)
for _, transport := range []string{"stdio", "http"} {
t.Run(transport, func(t *testing.T) {
before, baselineNames := schemaListPages(ctx, t, inv, false, transport, protocol, 7, schemaCache)
after, encodedNames := schemaListPages(ctx, t, inv, true, transport, protocol, 7, schemaCache)
before, baselineNames := schemaListPages(ctx, t, inv, false, transport, protocol, 7, schemaCache, sel.ui)
after, encodedNames := schemaListPages(ctx, t, inv, true, transport, protocol, 7, schemaCache, sel.ui)
assert.Equal(t, before, after, "complete JSON-RPC wire responses, including cursors and errors")
assert.Equal(t, wantNames, baselineNames)
assert.Equal(t, wantNames, encodedNames)
Expand Down
6 changes: 5 additions & 1 deletion pkg/github/typed_copilot_ui_outputs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,11 @@ func typedCopilotUISession(t *testing.T, deps ToolDependencies, protocol string,
if version == "" || version == "unknown" {
version = inventory.ProtocolVersionMultiRoundTrip
}
session := connectCommentVisibilityClient(t, server, version)
caps := &mcp.ClientCapabilities{}
caps.AddExtension("io.modelcontextprotocol/ui", map[string]any{
"mimeTypes": []string{MCPAppMIMEType},
})
session := connectCommentVisibilityClientWithCapabilities(t, server, version, caps)
list, err := session.ListTools(context.Background(), nil)
require.NoError(t, err)
require.Len(t, list.Tools, len(tools))
Expand Down
86 changes: 86 additions & 0 deletions pkg/github/ui_tools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ import (
"github.com/github/github-mcp-server/v2/internal/githubv4mock"
"github.com/github/github-mcp-server/v2/internal/toolsnaps"
ghcontext "github.com/github/github-mcp-server/v2/pkg/context"
"github.com/github/github-mcp-server/v2/pkg/inventory"
"github.com/github/github-mcp-server/v2/pkg/translations"
"github.com/google/go-github/v92/github"
"github.com/google/jsonschema-go/jsonschema"
"github.com/modelcontextprotocol/go-sdk/mcp"
"github.com/shurcooL/githubv4"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -602,3 +604,87 @@ func Test_UIGet_OmittedForClientsWithoutUISupport(t *testing.T) {
assert.True(t, hasUIGet(ghcontext.WithUISupport(context.Background(), true)))
assert.True(t, hasUIGet(context.Background()), "ui_get is kept when UI support is unknown")
}

// Test_UIGet_ListedOnlyWhenClientAdvertisesMCPAppsCapability is the remote-server
// path: tools/list carries client capabilities in MCPMethodInfo, not the
// HasUISupport flag. Clients without extensions["io.modelcontextprotocol/ui"]
// must not see the app-only ui_get tool.
func Test_UIGet_ListedOnlyWhenClientAdvertisesMCPAppsCapability(t *testing.T) {
inv, err := NewInventory(translations.NullTranslationHelper).WithToolsets([]string{"all"}).Build()
require.NoError(t, err)

hasUIGet := func(caps *mcp.ClientCapabilities) bool {
ctx := ghcontext.WithMCPMethodInfo(context.Background(), &ghcontext.MCPMethodInfo{
Method: "tools/list",
ClientCapabilities: caps,
})
for _, tool := range inv.ToolsForRegistration(ctx) {
if tool.Tool.Name == "ui_get" {
return true
}
}
return false
}

withoutApps := &mcp.ClientCapabilities{}
withApps := &mcp.ClientCapabilities{}
withApps.AddExtension("io.modelcontextprotocol/ui", map[string]any{
"mimeTypes": []string{"text/html;profile=mcp-app"},
})

assert.False(t, hasUIGet(withoutApps),
"ui_get must not be listed to clients that omit the MCP Apps capability")
assert.True(t, hasUIGet(withApps),
"ui_get must be listed to clients that advertise the MCP Apps capability")
}

// Test_UIGet_ListedOnStdioSessionOnlyWhenClientAdvertisesMCPApps is the stdio
// path: tools are registered before initialize, so client capabilities are not
// available at RegisterTools. App-only ui_get must still be omitted from
// tools/list unless that session advertised MCP Apps.
func Test_UIGet_ListedOnStdioSessionOnlyWhenClientAdvertisesMCPApps(t *testing.T) {
inv, err := inventory.NewBuilder().
SetTools([]inventory.ServerTool{UIGet(translations.NullTranslationHelper)}).
WithToolsets([]string{"all"}).
Build()
require.NoError(t, err)

listHasUIGet := func(t *testing.T, caps *mcp.ClientCapabilities) bool {
t.Helper()
server := mcp.NewServer(&mcp.Implementation{Name: "stdio-ui-get", Version: "v1"}, nil)
// context.Background() matches stdio: capabilities are unknown at registration.
inv.RegisterTools(context.Background(), server, nil)

serverTransport, clientTransport := mcp.NewInMemoryTransports()
serverSession, err := server.Connect(context.Background(), serverTransport, nil)
require.NoError(t, err)
t.Cleanup(func() { _ = serverSession.Close() })

client := mcp.NewClient(&mcp.Implementation{Name: "test-client", Version: "v1"}, &mcp.ClientOptions{
Capabilities: caps,
})
clientSession, err := client.Connect(context.Background(), clientTransport, nil)
require.NoError(t, err)
t.Cleanup(func() { _ = clientSession.Close() })

list, err := clientSession.ListTools(context.Background(), nil)
require.NoError(t, err)
for _, tool := range list.Tools {
if tool.Name == "ui_get" {
return true
}
}
return false
}

withoutApps := &mcp.ClientCapabilities{}
withApps := &mcp.ClientCapabilities{}
withApps.AddExtension("io.modelcontextprotocol/ui", map[string]any{
"mimeTypes": []string{MCPAppMIMEType},
})

assert.False(t, listHasUIGet(t, withoutApps),
"ui_get must not be listed to a stdio client that omits the MCP Apps capability")
assert.True(t, listHasUIGet(t, withApps),
"ui_get must be listed to a stdio client that advertises the MCP Apps capability")
}
61 changes: 51 additions & 10 deletions pkg/inventory/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,13 +182,15 @@ func (r *Inventory) ToolsetDescriptions() map[ToolsetID]string {
// diagnostics that need the same view of the tool surface the server would
// register.
//
// MCP Apps UI metadata is stripped only when the client explicitly did not
// MCP Apps UI metadata is stripped when the client explicitly did not
// advertise the io.modelcontextprotocol/ui extension capability (per the
// 2026-01-26 MCP Apps spec, servers SHOULD check client capabilities before
// exposing UI-enabled tools). In that case app-only tools (whose
// _meta.ui.visibility excludes "model") are omitted entirely. When the
// capability is unknown (e.g. stdio paths that do not populate the context
// flag) the metadata is kept.
// exposing UI-enabled tools). That is detected from the HasUISupport context
// flag or from parsed ClientCapabilities on the request. In that case
// app-only tools (whose _meta.ui.visibility excludes "model") are omitted
// entirely. When the capability is unknown at registration (e.g. stdio, which
// registers before initialize) the metadata is kept; app-only tools are then
// filtered per session at tools/list from the client's declared capabilities.
func (r *Inventory) ToolsForRegistration(ctx context.Context) []ServerTool {
ctx = WithFeatureState(ctx, r.featureChecker)
tools := r.availableTools(ctx)
Expand Down Expand Up @@ -238,12 +240,48 @@ func appendFeatureDeclaration(features []FeatureFlag, rule FeatureRule) []Featur
return features
}

// mcpAppsUIExtensionKey is the client capability extension that advertises
// MCP Apps UI support (MCP Apps 2026-01-26 spec).
const mcpAppsUIExtensionKey = "io.modelcontextprotocol/ui"

// shouldStripMCPAppsMetadata centralises the strip decision so the same logic
// is exercised by tests and by RegisterTools. Metadata is stripped only when
// the client is known not to support MCP Apps UI.
// is exercised by tests and by RegisterTools. Metadata is stripped when the
// client is known not to support MCP Apps UI: either the HasUISupport context
// flag is explicitly false, or the request's parsed ClientCapabilities omit
// the io.modelcontextprotocol/ui extension. When neither source is present
// (e.g. stdio paths that register before initialize) the metadata is kept;
// list-time filtering still omits app-only tools for that session.
func shouldStripMCPAppsMetadata(ctx context.Context) bool {
supported, ok := ghcontext.HasUISupport(ctx)
return ok && !supported
if supported, ok := ghcontext.HasUISupport(ctx); ok {
return !supported
}
if info, ok := ghcontext.MCPMethod(ctx); ok && info != nil && info.ClientCapabilities != nil {
return !clientAdvertisesMCPApps(info.ClientCapabilities)
}
return false
}

// clientAdvertisesMCPApps reports whether caps include the MCP Apps UI
// extension. Nil or empty capabilities do not advertise support.
func clientAdvertisesMCPApps(caps *mcp.ClientCapabilities) bool {
if caps == nil {
return false
}
_, hasUI := caps.Extensions[mcpAppsUIExtensionKey]
return hasUI
}

// clientSupportsMCPAppsUI is the list-time capability check. Prefer explicit
// HasUISupport / parsed MCPMethodInfo (HTTP), then the request's session
// capabilities (stdio after initialize).
func clientSupportsMCPAppsUI(ctx context.Context, caps *mcp.ClientCapabilities) bool {
if supported, ok := ghcontext.HasUISupport(ctx); ok {
return supported
}
if info, ok := ghcontext.MCPMethod(ctx); ok && info != nil && info.ClientCapabilities != nil {
return clientAdvertisesMCPApps(info.ClientCapabilities)
}
return clientAdvertisesMCPApps(caps)
}

// RegisterTools registers all available tools with the server using the provided dependencies.
Expand All @@ -252,7 +290,9 @@ func shouldStripMCPAppsMetadata(ctx context.Context) bool {
// MCP Apps UI metadata (`_meta.ui`) is stripped from the registered tools when
// the client did not advertise the io.modelcontextprotocol/ui extension. The
// strip happens here (rather than at Build() time) so the per-request
// context, which carries the client capability, is in scope.
// context, which carries the client capability, is in scope. Stdio servers
// register before initialize, so app-only tools stay registered and are
// filtered per session at tools/list.
//
// Schema definitions must remain immutable and be reused across registrations.
// Their encodings are retained process-wide, keyed by source schema identity.
Expand All @@ -270,6 +310,7 @@ func (r *Inventory) RegisterToolsForProtocolEra(ctx context.Context, s *mcp.Serv
func (r *Inventory) registerTools(ctx context.Context, s *mcp.Server, deps any, era ProtocolEra, middleware ...ToolHandlerMiddleware) {
tools := r.ToolsForRegistration(ctx)
addToolAvailabilityMiddleware(s, tools)
addAppOnlyToolFilterMiddleware(s, tools)
schemas := make(map[string]listedToolSchemas, len(tools))
registrations := make(map[string]*typedToolRegistration, len(tools))
for _, tool := range tools {
Expand Down
29 changes: 27 additions & 2 deletions pkg/inventory/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2510,8 +2510,9 @@ func captureRegisteredTools(ctx context.Context, t *testing.T, reg *Inventory) [
}

// TestShouldStripMCPAppsMetadata verifies the spec-conformant strip decision:
// strip only when the client explicitly does not advertise the
// io.modelcontextprotocol/ui extension.
// strip when the client explicitly does not advertise the
// io.modelcontextprotocol/ui extension, via HasUISupport or parsed
// ClientCapabilities.
func TestShouldStripMCPAppsMetadata(t *testing.T) {
t.Parallel()

Expand All @@ -2535,6 +2536,30 @@ func TestShouldStripMCPAppsMetadata(t *testing.T) {
setupCtx: func() context.Context { return ghcontext.WithUISupport(context.Background(), false) },
want: true,
},
{
name: "parsed capabilities omit MCP Apps extension -> strip",
setupCtx: func() context.Context {
return ghcontext.WithMCPMethodInfo(context.Background(), &ghcontext.MCPMethodInfo{
Method: "tools/list",
ClientCapabilities: &mcp.ClientCapabilities{},
})
},
want: true,
},
{
name: "parsed capabilities advertise MCP Apps extension -> keep",
setupCtx: func() context.Context {
caps := &mcp.ClientCapabilities{}
caps.AddExtension("io.modelcontextprotocol/ui", map[string]any{
"mimeTypes": []string{"text/html;profile=mcp-app"},
})
return ghcontext.WithMCPMethodInfo(context.Background(), &ghcontext.MCPMethodInfo{
Method: "tools/list",
ClientCapabilities: caps,
})
},
want: false,
},
}

for _, tc := range tests {
Expand Down
49 changes: 49 additions & 0 deletions pkg/inventory/tool_availability.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,52 @@ func toolUnavailableResult(name string, req *mcp.CallToolRequest, availability t
IsError: true,
}
}

// addAppOnlyToolFilterMiddleware omits app-only tools from tools/list when
// the connected client has not advertised MCP Apps UI support. Stdio registers
// tools before initialize, so this session-level filter is what hides ui_get
// (and other _meta.ui.visibility:["app"] tools) from non-Apps clients. Tools
// remain callable so MCP App views can still invoke them.
func addAppOnlyToolFilterMiddleware(server *mcp.Server, tools []ServerTool) {
appOnly := make(map[string]struct{})
for i := range tools {
if isAppOnlyTool(&tools[i]) {
appOnly[tools[i].Tool.Name] = struct{}{}
}
}
if len(appOnly) == 0 {
return
}
server.AddReceivingMiddleware(appOnlyToolFilterMiddleware(appOnly))
}

func appOnlyToolFilterMiddleware(appOnly map[string]struct{}) mcp.Middleware {
return func(next mcp.MethodHandler) mcp.MethodHandler {
return func(ctx context.Context, method string, request mcp.Request) (mcp.Result, error) {
req, ok := request.(*mcp.ListToolsRequest)
if !ok {
return next(ctx, method, request)
}
result, err := next(ctx, method, request)
if err != nil {
return nil, err
}
if clientSupportsMCPAppsUI(ctx, req.ClientCapabilities()) {
return result, nil
}
list, ok := result.(*mcp.ListToolsResult)
if !ok {
return result, nil
}
filtered := make([]*mcp.Tool, 0, len(list.Tools))
for _, tool := range list.Tools {
if _, skip := appOnly[tool.Name]; skip {
continue
}
filtered = append(filtered, tool)
}
list.Tools = filtered
return list, nil
}
}
}
Loading