chore: import upstream snapshot with attribution
This commit is contained in:
@@ -0,0 +1,19 @@
|
||||
package context
|
||||
|
||||
import "context"
|
||||
|
||||
// graphQLFeaturesKey is a context key for GraphQL feature flags
|
||||
type graphQLFeaturesKey struct{}
|
||||
|
||||
// withGraphQLFeatures adds GraphQL feature flags to the context
|
||||
func WithGraphQLFeatures(ctx context.Context, features ...string) context.Context {
|
||||
return context.WithValue(ctx, graphQLFeaturesKey{}, features)
|
||||
}
|
||||
|
||||
// GetGraphQLFeatures retrieves GraphQL feature flags from the context
|
||||
func GetGraphQLFeatures(ctx context.Context) []string {
|
||||
if features, ok := ctx.Value(graphQLFeaturesKey{}).([]string); ok {
|
||||
return features
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package context
|
||||
|
||||
import "context"
|
||||
|
||||
type mcpMethodInfoCtx string
|
||||
|
||||
var mcpMethodInfoCtxKey mcpMethodInfoCtx = "mcpmethodinfo"
|
||||
|
||||
// MCPMethodInfo contains pre-parsed MCP method information extracted from the JSON-RPC request.
|
||||
// This is populated early in the request lifecycle to enable:
|
||||
// - Inventory filtering via ForMCPRequest (only register needed tools/resources/prompts)
|
||||
// - Avoiding duplicate JSON parsing in middlewares (secret-scanning, scope-challenge)
|
||||
// - Performance optimization for per-request server creation
|
||||
type MCPMethodInfo struct {
|
||||
// Method is the MCP method being called (e.g., "tools/call", "tools/list", "initialize")
|
||||
Method string
|
||||
// ItemName is the name of the specific item being accessed (tool name, resource URI, prompt name)
|
||||
// Only populated for call/get methods (tools/call, prompts/get, resources/read)
|
||||
ItemName string
|
||||
// Owner is the repository owner from tool call arguments, if present
|
||||
Owner string
|
||||
// Repo is the repository name from tool call arguments, if present
|
||||
Repo string
|
||||
// Arguments contains the raw tool arguments for tools/call requests
|
||||
Arguments map[string]any
|
||||
}
|
||||
|
||||
// WithMCPMethodInfo stores the MCPMethodInfo in the context.
|
||||
func WithMCPMethodInfo(ctx context.Context, info *MCPMethodInfo) context.Context {
|
||||
return context.WithValue(ctx, mcpMethodInfoCtxKey, info)
|
||||
}
|
||||
|
||||
// MCPMethod retrieves the MCPMethodInfo from the context.
|
||||
func MCPMethod(ctx context.Context) (*MCPMethodInfo, bool) {
|
||||
if info, ok := ctx.Value(mcpMethodInfoCtxKey).(*MCPMethodInfo); ok {
|
||||
return info, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
@@ -0,0 +1,131 @@
|
||||
package context
|
||||
|
||||
import "context"
|
||||
|
||||
// readonlyCtxKey is a context key for read-only mode
|
||||
type readonlyCtxKey struct{}
|
||||
|
||||
// WithReadonly adds read-only mode state to the context
|
||||
func WithReadonly(ctx context.Context, enabled bool) context.Context {
|
||||
return context.WithValue(ctx, readonlyCtxKey{}, enabled)
|
||||
}
|
||||
|
||||
// IsReadonly retrieves the read-only mode state from the context
|
||||
func IsReadonly(ctx context.Context) bool {
|
||||
if enabled, ok := ctx.Value(readonlyCtxKey{}).(bool); ok {
|
||||
return enabled
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// toolsetsCtxKey is a context key for the active toolsets
|
||||
type toolsetsCtxKey struct{}
|
||||
|
||||
// WithToolsets adds the active toolsets to the context
|
||||
func WithToolsets(ctx context.Context, toolsets []string) context.Context {
|
||||
return context.WithValue(ctx, toolsetsCtxKey{}, toolsets)
|
||||
}
|
||||
|
||||
// GetToolsets retrieves the active toolsets from the context
|
||||
func GetToolsets(ctx context.Context) []string {
|
||||
if toolsets, ok := ctx.Value(toolsetsCtxKey{}).([]string); ok {
|
||||
return toolsets
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// toolsCtxKey is a context key for tools
|
||||
type toolsCtxKey struct{}
|
||||
|
||||
// WithTools adds the tools to the context
|
||||
func WithTools(ctx context.Context, tools []string) context.Context {
|
||||
return context.WithValue(ctx, toolsCtxKey{}, tools)
|
||||
}
|
||||
|
||||
// GetTools retrieves the tools from the context
|
||||
func GetTools(ctx context.Context) []string {
|
||||
if tools, ok := ctx.Value(toolsCtxKey{}).([]string); ok {
|
||||
return tools
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// lockdownCtxKey is a context key for lockdown mode
|
||||
type lockdownCtxKey struct{}
|
||||
|
||||
// WithLockdownMode adds lockdown mode state to the context
|
||||
func WithLockdownMode(ctx context.Context, enabled bool) context.Context {
|
||||
return context.WithValue(ctx, lockdownCtxKey{}, enabled)
|
||||
}
|
||||
|
||||
// IsLockdownMode retrieves the lockdown mode state from the context
|
||||
func IsLockdownMode(ctx context.Context) bool {
|
||||
if enabled, ok := ctx.Value(lockdownCtxKey{}).(bool); ok {
|
||||
return enabled
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// insidersCtxKey is a context key for insiders mode
|
||||
type insidersCtxKey struct{}
|
||||
|
||||
// WithInsidersMode adds insiders mode state to the context
|
||||
func WithInsidersMode(ctx context.Context, enabled bool) context.Context {
|
||||
return context.WithValue(ctx, insidersCtxKey{}, enabled)
|
||||
}
|
||||
|
||||
// IsInsidersMode retrieves the insiders mode state from the context
|
||||
func IsInsidersMode(ctx context.Context) bool {
|
||||
if enabled, ok := ctx.Value(insidersCtxKey{}).(bool); ok {
|
||||
return enabled
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// excludeToolsCtxKey is a context key for excluded tools
|
||||
type excludeToolsCtxKey struct{}
|
||||
|
||||
// WithExcludeTools adds the excluded tools to the context
|
||||
func WithExcludeTools(ctx context.Context, tools []string) context.Context {
|
||||
return context.WithValue(ctx, excludeToolsCtxKey{}, tools)
|
||||
}
|
||||
|
||||
// GetExcludeTools retrieves the excluded tools from the context
|
||||
func GetExcludeTools(ctx context.Context) []string {
|
||||
if tools, ok := ctx.Value(excludeToolsCtxKey{}).([]string); ok {
|
||||
return tools
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// headerFeaturesCtxKey is a context key for raw header feature flags
|
||||
type headerFeaturesCtxKey struct{}
|
||||
|
||||
// WithHeaderFeatures stores the raw feature flags from the X-MCP-Features header into context
|
||||
func WithHeaderFeatures(ctx context.Context, features []string) context.Context {
|
||||
return context.WithValue(ctx, headerFeaturesCtxKey{}, features)
|
||||
}
|
||||
|
||||
// GetHeaderFeatures retrieves the raw feature flags from context
|
||||
func GetHeaderFeatures(ctx context.Context) []string {
|
||||
if features, ok := ctx.Value(headerFeaturesCtxKey{}).([]string); ok {
|
||||
return features
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// uiSupportCtxKey is a context key for MCP Apps UI support
|
||||
type uiSupportCtxKey struct{}
|
||||
|
||||
// WithUISupport stores whether the client supports MCP Apps UI in the context.
|
||||
// This is used by HTTP/stateless servers where the go-sdk session may not
|
||||
// persist client capabilities across requests.
|
||||
func WithUISupport(ctx context.Context, supported bool) context.Context {
|
||||
return context.WithValue(ctx, uiSupportCtxKey{}, supported)
|
||||
}
|
||||
|
||||
// HasUISupport retrieves the MCP Apps UI support flag from context.
|
||||
func HasUISupport(ctx context.Context) (supported bool, ok bool) {
|
||||
v, ok := ctx.Value(uiSupportCtxKey{}).(bool)
|
||||
return v, ok
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
package context
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/github/github-mcp-server/pkg/utils"
|
||||
)
|
||||
|
||||
type tokenCtxKey struct{}
|
||||
|
||||
type TokenInfo struct {
|
||||
Token string
|
||||
TokenType utils.TokenType
|
||||
}
|
||||
|
||||
// WithTokenInfo adds TokenInfo to the context
|
||||
func WithTokenInfo(ctx context.Context, tokenInfo *TokenInfo) context.Context {
|
||||
return context.WithValue(ctx, tokenCtxKey{}, tokenInfo)
|
||||
}
|
||||
|
||||
// GetTokenInfo retrieves the authentication token from the context
|
||||
func GetTokenInfo(ctx context.Context) (*TokenInfo, bool) {
|
||||
if tokenInfo, ok := ctx.Value(tokenCtxKey{}).(*TokenInfo); ok {
|
||||
return tokenInfo, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
type tokenScopesKey struct{}
|
||||
|
||||
// WithTokenScopes adds token scopes to the context
|
||||
func WithTokenScopes(ctx context.Context, scopes []string) context.Context {
|
||||
return context.WithValue(ctx, tokenScopesKey{}, scopes)
|
||||
}
|
||||
|
||||
// GetTokenScopes retrieves token scopes from the context
|
||||
func GetTokenScopes(ctx context.Context) ([]string, bool) {
|
||||
if scopes, ok := ctx.Value(tokenScopesKey{}).([]string); ok {
|
||||
return scopes, true
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
Reference in New Issue
Block a user