Documentation
¶
Index ¶
- func AuthMiddleware(config Config, logger forge.Logger) forge.Middleware
- func NewExtension(opts ...ConfigOption) forge.Extension
- func NewExtensionWithConfig(config Config) forge.Extension
- func RateLimitMiddleware(config Config, logger forge.Logger, metrics forge.Metrics) forge.Middleware
- type CallToolRequest
- type CallToolResponse
- type Capabilities
- type Config
- type ConfigOption
- func WithAuth(authHeader string, tokens []string) ConfigOption
- func WithAutoExposeRoutes(autoExpose bool) ConfigOption
- func WithBasePath(basePath string) ConfigOption
- func WithConfig(config Config) ConfigOption
- func WithEnabled(enabled bool) ConfigOption
- func WithExcludePatterns(patterns []string) ConfigOption
- func WithIncludePatterns(patterns []string) ConfigOption
- func WithPrompts(enable bool) ConfigOption
- func WithRateLimit(limit int) ConfigOption
- func WithRequireConfig(require bool) ConfigOption
- func WithResources(enable bool) ConfigOption
- func WithServerInfo(name, version string) ConfigOption
- func WithToolPrefix(prefix string) ConfigOption
- type Content
- type Extension
- type GetPromptRequest
- type GetPromptResponse
- type JSONSchema
- type ListPromptsResponse
- type ListResourcesResponse
- type ListToolsResponse
- type Prompt
- type PromptArgument
- type PromptGenerator
- type PromptMessage
- type PromptsCapability
- type RateLimiter
- type ReadResourceRequest
- type ReadResourceResponse
- type Resource
- type ResourceContents
- type ResourceReader
- type ResourcesCapability
- type Server
- func (s *Server) CacheSchema(key string, schema *JSONSchema)
- func (s *Server) Clear()
- func (s *Server) ExecuteTool(ctx context.Context, tool *Tool, arguments map[string]interface{}) (string, error)
- func (s *Server) GeneratePrompt(ctx context.Context, prompt *Prompt, args map[string]interface{}) ([]PromptMessage, error)
- func (s *Server) GenerateToolFromRoute(route forge.RouteInfo) (*Tool, error)
- func (s *Server) GetCachedSchema(key string) (*JSONSchema, bool)
- func (s *Server) GetPrompt(name string) (*Prompt, error)
- func (s *Server) GetResource(uri string) (*Resource, error)
- func (s *Server) GetServerInfo() ServerInfo
- func (s *Server) GetTool(name string) (*Tool, error)
- func (s *Server) ListPrompts() []Prompt
- func (s *Server) ListResources() []Resource
- func (s *Server) ListTools() []Tool
- func (s *Server) ReadResource(ctx context.Context, resource *Resource) (Content, error)
- func (s *Server) RegisterPrompt(prompt *Prompt) error
- func (s *Server) RegisterPromptGenerator(name string, generator PromptGenerator) error
- func (s *Server) RegisterResource(resource *Resource) error
- func (s *Server) RegisterResourceReader(uri string, reader ResourceReader) error
- func (s *Server) RegisterTool(tool *Tool) error
- func (s *Server) Stats() map[string]interface{}
- type ServerInfo
- type Tool
- type ToolCall
- type ToolResult
- type ToolsCapability
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AuthMiddleware ¶
func AuthMiddleware(config Config, logger forge.Logger) forge.Middleware
AuthMiddleware creates middleware for MCP authentication
func NewExtension ¶
func NewExtension(opts ...ConfigOption) forge.Extension
NewExtension creates a new MCP extension with functional options. Config is loaded from ConfigManager by default, with options providing overrides.
Example:
// Load from ConfigManager (tries "extensions.mcp", then "mcp")
mcp.NewExtension()
// Override specific fields
mcp.NewExtension(
mcp.WithEnabled(true),
mcp.WithBasePath("/api/mcp"),
)
// Require config from ConfigManager
mcp.NewExtension(mcp.WithRequireConfig(true))
func NewExtensionWithConfig ¶
NewExtensionWithConfig creates a new MCP extension with a complete config. This is for backward compatibility or when config is fully known at initialization.
func RateLimitMiddleware ¶
func RateLimitMiddleware(config Config, logger forge.Logger, metrics forge.Metrics) forge.Middleware
RateLimitMiddleware creates middleware for rate limiting
Types ¶
type CallToolRequest ¶
type CallToolRequest struct {
Name string `json:"name"`
Arguments map[string]interface{} `json:"arguments,omitempty"`
}
CallToolRequest represents a request to call a tool
type CallToolResponse ¶
type CallToolResponse struct {
Content []Content `json:"content"`
IsError bool `json:"isError,omitempty"`
}
CallToolResponse represents the response from calling a tool
type Capabilities ¶
type Capabilities struct {
Tools *ToolsCapability `json:"tools,omitempty"`
Resources *ResourcesCapability `json:"resources,omitempty"`
Prompts *PromptsCapability `json:"prompts,omitempty"`
}
Capabilities represents MCP server capabilities
type Config ¶
type Config struct {
// Enabled enables or disables the MCP server
Enabled bool
// BasePath is the base path for MCP endpoints
// Default: "/_/mcp"
BasePath string
// ServerName is the name of the MCP server
// Default: app name
ServerName string
// ServerVersion is the version of the MCP server
// Default: app version
ServerVersion string
// AutoExposeRoutes automatically exposes routes as MCP tools
// Default: true
AutoExposeRoutes bool
// ToolPrefix is a prefix to add to all tool names
// Example: "myapi_" -> "myapi_create_user"
ToolPrefix string
// ExcludePatterns are route patterns to exclude from MCP
// Example: []string{"/_/*", "/internal/*"}
ExcludePatterns []string
// IncludePatterns are route patterns to explicitly include in MCP
// If set, only matching routes are exposed
IncludePatterns []string
// MaxToolNameLength is the maximum length for tool names
// Default: 64
MaxToolNameLength int
// EnableResources enables MCP resources (data access)
// Default: false
EnableResources bool
// EnablePrompts enables MCP prompts (templates)
// Default: false
EnablePrompts bool
// RequireAuth requires authentication for MCP endpoints
// Default: false
RequireAuth bool
// AuthHeader is the header to check for authentication
// Default: "Authorization"
AuthHeader string
// AuthTokens are valid authentication tokens
// Only used if RequireAuth is true
AuthTokens []string
// RateLimitPerMinute limits MCP tool calls per minute
// 0 means unlimited
// Default: 0
RateLimitPerMinute int
// EnableMetrics enables metrics collection for MCP operations
// Default: true
EnableMetrics bool
// SchemaCache enables caching of generated JSON schemas
// Default: true
SchemaCache bool
// RequireConfig determines if config must exist in ConfigManager
// If true, extension fails to start without config
// If false, uses defaults when config is missing
RequireConfig bool
}
Config configures the MCP extension
func (Config) ShouldExpose ¶
ShouldExpose determines if a route should be exposed as an MCP tool
type ConfigOption ¶
type ConfigOption func(*Config)
ConfigOption is a functional option for configuring the MCP extension
func WithAuth ¶
func WithAuth(authHeader string, tokens []string) ConfigOption
WithAuth enables authentication with tokens
func WithAutoExposeRoutes ¶
func WithAutoExposeRoutes(autoExpose bool) ConfigOption
WithAutoExposeRoutes sets whether to automatically expose routes
func WithBasePath ¶
func WithBasePath(basePath string) ConfigOption
WithBasePath sets the base path for MCP endpoints
func WithEnabled ¶
func WithEnabled(enabled bool) ConfigOption
WithEnabled sets whether MCP is enabled
func WithExcludePatterns ¶
func WithExcludePatterns(patterns []string) ConfigOption
WithExcludePatterns sets patterns to exclude from MCP
func WithIncludePatterns ¶
func WithIncludePatterns(patterns []string) ConfigOption
WithIncludePatterns sets patterns to include in MCP
func WithRateLimit ¶
func WithRateLimit(limit int) ConfigOption
WithRateLimit sets the rate limit per minute
func WithRequireConfig ¶
func WithRequireConfig(require bool) ConfigOption
WithRequireConfig sets whether config is required from ConfigManager
func WithResources ¶
func WithResources(enable bool) ConfigOption
WithResources enables MCP resources
func WithServerInfo ¶
func WithServerInfo(name, version string) ConfigOption
WithServerInfo sets server name and version
func WithToolPrefix ¶
func WithToolPrefix(prefix string) ConfigOption
WithToolPrefix sets the tool prefix
type Content ¶
type Content struct {
Type string `json:"type"` // "text", "image", "resource"
Text string `json:"text,omitempty"`
Data string `json:"data,omitempty"`
MimeType string `json:"mimeType,omitempty"`
}
Content represents a piece of content in MCP
type Extension ¶
type Extension struct {
*forge.BaseExtension
// contains filtered or unexported fields
}
Extension implements forge.Extension for MCP (Model Context Protocol) server
type GetPromptRequest ¶
type GetPromptRequest struct {
Name string `json:"name"`
Arguments map[string]interface{} `json:"arguments,omitempty"`
}
GetPromptRequest represents a request to get a prompt
type GetPromptResponse ¶
type GetPromptResponse struct {
Description string `json:"description,omitempty"`
Messages []PromptMessage `json:"messages"`
}
GetPromptResponse represents the response from getting a prompt
type JSONSchema ¶
type JSONSchema struct {
Type string `json:"type,omitempty"`
Properties map[string]*JSONSchema `json:"properties,omitempty"`
Items *JSONSchema `json:"items,omitempty"`
Required []string `json:"required,omitempty"`
Description string `json:"description,omitempty"`
Enum []interface{} `json:"enum,omitempty"`
Format string `json:"format,omitempty"`
Pattern string `json:"pattern,omitempty"`
MinLength *int `json:"minLength,omitempty"`
MaxLength *int `json:"maxLength,omitempty"`
Minimum *float64 `json:"minimum,omitempty"`
Maximum *float64 `json:"maximum,omitempty"`
AdditionalProperties interface{} `json:"additionalProperties,omitempty"`
}
JSONSchema represents a JSON Schema for input/output validation
type ListPromptsResponse ¶
type ListPromptsResponse struct {
Prompts []Prompt `json:"prompts"`
}
ListPromptsResponse represents the response for listing prompts
type ListResourcesResponse ¶
type ListResourcesResponse struct {
Resources []Resource `json:"resources"`
}
ListResourcesResponse represents the response for listing resources
type ListToolsResponse ¶
type ListToolsResponse struct {
Tools []Tool `json:"tools"`
}
ListToolsResponse represents the response for listing tools
type Prompt ¶
type Prompt struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Arguments []PromptArgument `json:"arguments,omitempty"`
}
Prompt represents an MCP prompt template
type PromptArgument ¶
type PromptArgument struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
Required bool `json:"required,omitempty"`
}
PromptArgument represents an argument for a prompt
type PromptGenerator ¶
type PromptGenerator func(ctx context.Context, prompt *Prompt, args map[string]interface{}) ([]PromptMessage, error)
PromptGenerator is a function that generates prompt messages
type PromptMessage ¶
type PromptMessage struct {
Role string `json:"role"` // "user" or "assistant"
Content []Content `json:"content"`
}
PromptMessage represents a message in a prompt result
type PromptsCapability ¶
type PromptsCapability struct {
ListChanged bool `json:"listChanged,omitempty"`
}
PromptsCapability represents prompts capability
type RateLimiter ¶
type RateLimiter struct {
// contains filtered or unexported fields
}
RateLimiter tracks request rates
func NewRateLimiter ¶
func NewRateLimiter(config Config, logger forge.Logger) *RateLimiter
NewRateLimiter creates a new rate limiter
func (*RateLimiter) Allow ¶
func (rl *RateLimiter) Allow(clientID string) bool
Allow checks if a request is allowed
func (*RateLimiter) GetInfo ¶
func (rl *RateLimiter) GetInfo(clientID string) *requestInfo
GetInfo returns rate limit info for a client
func (*RateLimiter) Stop ¶
func (rl *RateLimiter) Stop()
Stop stops the rate limiter cleanup goroutine
type ReadResourceRequest ¶
type ReadResourceRequest struct {
URI string `json:"uri"`
}
ReadResourceRequest represents a request to read a resource
type ReadResourceResponse ¶
type ReadResourceResponse struct {
Contents []Content `json:"contents"`
}
ReadResourceResponse represents the response from reading a resource
type Resource ¶
type Resource struct {
URI string `json:"uri"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
MimeType string `json:"mimeType,omitempty"`
Schema *JSONSchema `json:"schema,omitempty"`
}
Resource represents an MCP resource (data access)
type ResourceContents ¶
ResourceContents represents the contents of a resource
type ResourceReader ¶
ResourceReader is a function that reads resource content
type ResourcesCapability ¶
type ResourcesCapability struct {
Subscribe bool `json:"subscribe,omitempty"`
ListChanged bool `json:"listChanged,omitempty"`
}
ResourcesCapability represents resources capability
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server represents an MCP server that exposes tools, resources, and prompts
func (*Server) CacheSchema ¶
func (s *Server) CacheSchema(key string, schema *JSONSchema)
CacheSchema caches a generated JSON schema
func (*Server) Clear ¶
func (s *Server) Clear()
Clear clears all registered tools, resources, and prompts
func (*Server) ExecuteTool ¶
func (s *Server) ExecuteTool(ctx context.Context, tool *Tool, arguments map[string]interface{}) (string, error)
ExecuteTool executes a tool by calling its underlying route
func (*Server) GeneratePrompt ¶
func (s *Server) GeneratePrompt(ctx context.Context, prompt *Prompt, args map[string]interface{}) ([]PromptMessage, error)
GeneratePrompt generates prompt messages using registered generator or default
func (*Server) GenerateToolFromRoute ¶
GenerateToolFromRoute generates an MCP tool from a Forge route
func (*Server) GetCachedSchema ¶
func (s *Server) GetCachedSchema(key string) (*JSONSchema, bool)
GetCachedSchema retrieves a cached schema
func (*Server) GetResource ¶
GetResource retrieves a resource by URI
func (*Server) GetServerInfo ¶
func (s *Server) GetServerInfo() ServerInfo
GetServerInfo returns information about the MCP server
func (*Server) ListPrompts ¶
ListPrompts returns all registered prompts
func (*Server) ListResources ¶
ListResources returns all registered resources
func (*Server) ReadResource ¶
ReadResource reads resource content using registered reader or default
func (*Server) RegisterPrompt ¶
RegisterPrompt registers a new MCP prompt
func (*Server) RegisterPromptGenerator ¶
func (s *Server) RegisterPromptGenerator(name string, generator PromptGenerator) error
RegisterPromptGenerator registers a custom generator for a prompt
func (*Server) RegisterResource ¶
RegisterResource registers a new MCP resource
func (*Server) RegisterResourceReader ¶
func (s *Server) RegisterResourceReader(uri string, reader ResourceReader) error
RegisterResourceReader registers a custom reader for a resource
func (*Server) RegisterTool ¶
RegisterTool registers a new MCP tool
type ServerInfo ¶
type ServerInfo struct {
Name string `json:"name"`
Version string `json:"version"`
Capabilities Capabilities `json:"capabilities"`
}
ServerInfo represents MCP server information
type Tool ¶
type Tool struct {
Name string `json:"name"`
Description string `json:"description"`
InputSchema *JSONSchema `json:"inputSchema"`
}
Tool represents an MCP tool that can be called by AI assistants
type ToolCall ¶
type ToolCall struct {
Name string `json:"name"`
Arguments map[string]interface{} `json:"arguments"`
}
ToolCall represents a request to execute a tool
type ToolResult ¶
type ToolResult struct {
Content []Content `json:"content"`
IsError bool `json:"isError,omitempty"`
}
ToolResult represents the result of a tool execution
type ToolsCapability ¶
type ToolsCapability struct {
ListChanged bool `json:"listChanged,omitempty"`
}
ToolsCapability represents tools capability