豆豆友情提示:这是一个非官方 GitHub 代理镜像,主要用于网络测试或访问加速。请勿在此进行登录、注册或处理任何敏感信息。进行这些操作请务必访问官方网站 github.com。 Raw 内容也通过此代理提供。
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions pkg/http/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ import (
"github.com/modelcontextprotocol/go-sdk/mcp"
)

// WithSchemaCache sets a shared SchemaCache for the handler.
// This avoids repeated schema reflection when a new MCP Server is created per request.
func WithSchemaCache(cache *mcp.SchemaCache) HandlerOption {
return func(o *HandlerOptions) {
o.SchemaCache = cache
}
}

type InventoryFactoryFunc func(r *http.Request) (*inventory.Inventory, error)
type GitHubMCPServerFactoryFunc func(r *http.Request, deps github.ToolDependencies, inventory *inventory.Inventory, cfg *github.MCPServerConfig) (*mcp.Server, error)

Expand All @@ -31,6 +39,7 @@ type Handler struct {
inventoryFactoryFunc InventoryFactoryFunc
oauthCfg *oauth.Config
scopeFetcher scopes.FetcherInterface
schemaCache *mcp.SchemaCache
}

type HandlerOptions struct {
Expand All @@ -39,6 +48,7 @@ type HandlerOptions struct {
OAuthConfig *oauth.Config
ScopeFetcher scopes.FetcherInterface
FeatureChecker inventory.FeatureFlagChecker
SchemaCache *mcp.SchemaCache
}

type HandlerOption func(*HandlerOptions)
Expand Down Expand Up @@ -112,6 +122,7 @@ func NewHTTPMcpHandler(
inventoryFactoryFunc: inventoryFactory,
oauthCfg: opts.OAuthConfig,
scopeFetcher: scopeFetcher,
schemaCache: opts.SchemaCache,
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Do you think there would be any scenario where we don't want a schema cache for HTTP?

I feel like we can drop the WithSchemaCache when initializing (still keeping WithSchemaCache), and init an empty schema cache by default?
I guess the default right now is without a schema cache, but is that a good default? Maybe if you explicitly don't want one being able to pass nil, but that is difficult to manage with the current way that Options work

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Yeah that's a good question, I mean I don't think we want to make it optional, because in the end we're in control and we don't want it pluggable, it's our opinionated server after all. I'll spin off copilot to riff on that and see if we like it.

}
}

Expand Down Expand Up @@ -195,6 +206,7 @@ func (h *Handler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
Resources: &mcp.ResourceCapabilities{},
Prompts: &mcp.PromptCapabilities{},
}
so.SchemaCache = h.schemaCache
},
Comment thread
SamMorrowDrums marked this conversation as resolved.
},
})
Expand Down
7 changes: 6 additions & 1 deletion pkg/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/github/github-mcp-server/pkg/translations"
"github.com/github/github-mcp-server/pkg/utils"
"github.com/go-chi/chi/v5"
"github.com/modelcontextprotocol/go-sdk/mcp"
)

// knownFeatureFlags are the feature flags that can be enabled via X-MCP-Features header.
Expand Down Expand Up @@ -134,8 +135,12 @@ func RunHTTPServer(cfg ServerConfig) error {
serverOptions = append(serverOptions, WithScopeFetcher(scopeFetcher))
}

// Create a shared schema cache to avoid repeated JSON schema reflection
// when a new MCP Server is created per request in stateless mode.
schemaCache := mcp.NewSchemaCache()

r := chi.NewRouter()
handler := NewHTTPMcpHandler(ctx, &cfg, deps, t, logger, apiHost, append(serverOptions, WithFeatureChecker(featureChecker), WithOAuthConfig(oauthCfg))...)
handler := NewHTTPMcpHandler(ctx, &cfg, deps, t, logger, apiHost, append(serverOptions, WithFeatureChecker(featureChecker), WithOAuthConfig(oauthCfg), WithSchemaCache(schemaCache))...)
oauthHandler, err := oauth.NewAuthHandler(oauthCfg)
if err != nil {
return fmt.Errorf("failed to create OAuth handler: %w", err)
Expand Down
Loading