Documentation
¶
Overview ¶
Package mcpserver implements the MCP server functionality.
Index ¶
- Variables
- type MethodHandler
- type RegistrationServer
- func (s *RegistrationServer) GetServiceStatus(_ context.Context, _ *v1.GetServiceStatusRequest) (*v1.GetServiceStatusResponse, error)
- func (s *RegistrationServer) InitiateOAuth2Flow(_ context.Context, _ *v1.InitiateOAuth2FlowRequest) (*v1.InitiateOAuth2FlowResponse, error)
- func (s *RegistrationServer) ListServices(ctx context.Context, _ *v1.ListServicesRequest) (*v1.ListServicesResponse, error)
- func (s *RegistrationServer) RegisterService(ctx context.Context, req *v1.RegisterServiceRequest) (*v1.RegisterServiceResponse, error)
- func (s *RegistrationServer) RegisterTools(_ context.Context, _ *v1.RegisterToolsRequest) (*v1.RegisterToolsResponse, error)
- func (s *RegistrationServer) UnregisterService(_ context.Context, _ *v1.UnregisterServiceRequest) (*v1.UnregisterServiceResponse, error)
- type Router
- type Server
- func (s *Server) AddServiceInfo(serviceID string, info *tool.ServiceInfo)
- func (s *Server) AddTool(t tool.Tool) error
- func (s *Server) AuthManager() *auth.Manager
- func (s *Server) CallTool(ctx context.Context, req *tool.ExecutionRequest) (any, error)
- func (s *Server) ClearToolsForService(serviceKey string)
- func (s *Server) GetPrompt(ctx context.Context, req *mcp.GetPromptRequest) (*mcp.GetPromptResult, error)
- func (s *Server) GetServiceInfo(serviceID string) (*tool.ServiceInfo, bool)
- func (s *Server) GetTool(toolName string) (tool.Tool, bool)
- func (s *Server) ListPrompts(_ context.Context, _ *mcp.ListPromptsRequest) (*mcp.ListPromptsResult, error)
- func (s *Server) ListResources(_ context.Context, _ *mcp.ListResourcesRequest) (*mcp.ListResourcesResult, error)
- func (s *Server) ListTools() []tool.Tool
- func (s *Server) PromptManager() prompt.ManagerInterface
- func (s *Server) ReadResource(ctx context.Context, req *mcp.ReadResourceRequest) (*mcp.ReadResourceResult, error)
- func (s *Server) Reload() error
- func (s *Server) ResourceManager() resource.ManagerInterface
- func (s *Server) Server() *mcp.Server
- func (s *Server) ServiceRegistry() *serviceregistry.ServiceRegistry
- func (s *Server) SetMCPServer(mcpServer tool.MCPServerProvider)
- func (s *Server) SetReloadFunc(f func() error)
- func (s *Server) ToolManager() tool.ManagerInterface
Constants ¶
This section is empty.
Variables ¶
var AddReceivingMiddlewareHook func(name string)
AddReceivingMiddlewareHook is a hook for adding receiving middleware.
var NewRegistrationServerHook func(bus interface{}) (*RegistrationServer, error)
NewRegistrationServerHook is a test hook for overriding the creation of a RegistrationServer.
Functions ¶
This section is empty.
Types ¶
type MethodHandler ¶
MethodHandler defines the signature for a function that handles an MCP method call. It takes a context and a request, and returns a result and an error.
type RegistrationServer ¶
type RegistrationServer struct {
v1.UnimplementedRegistrationServiceServer
// contains filtered or unexported fields
}
RegistrationServer implements the gRPC server for service registration. It handles gRPC requests for registering and managing upstream services by publishing messages to the event bus and waiting for the results from the corresponding workers. This decouples the gRPC server from the core service registration logic, allowing for a more modular and scalable architecture.
func NewRegistrationServer ¶
func NewRegistrationServer(bus *bus.Provider) (*RegistrationServer, error)
NewRegistrationServer creates a new RegistrationServer initialized with the event bus.
The bus is used for communicating with the service registration workers, allowing for an asynchronous, decoupled registration process.
Parameters:
- bus: The event bus used for communication.
Returns:
- A new instance of the RegistrationServer.
- An error if the bus is nil.
func (*RegistrationServer) GetServiceStatus ¶
func (s *RegistrationServer) GetServiceStatus(_ context.Context, _ *v1.GetServiceStatusRequest) (*v1.GetServiceStatusResponse, error)
GetServiceStatus is not yet implemented. It is intended to handle requests for the status of a service.
Parameters:
- ctx: The context for the gRPC call.
- req: The request containing the service name or ID.
Returns:
- A response with the service status.
- An error (currently always Unimplemented).
func (*RegistrationServer) InitiateOAuth2Flow ¶
func (s *RegistrationServer) InitiateOAuth2Flow(_ context.Context, _ *v1.InitiateOAuth2FlowRequest) (*v1.InitiateOAuth2FlowResponse, error)
InitiateOAuth2Flow is not yet implemented. It is intended to handle the initiation of an OAuth2 flow for a service.
Parameters:
- ctx: The context for the gRPC call.
- req: The request containing OAuth2 flow details.
Returns:
- A response with the initiation result.
- An error (currently always Unimplemented).
func (*RegistrationServer) ListServices ¶
func (s *RegistrationServer) ListServices(ctx context.Context, _ *v1.ListServicesRequest) (*v1.ListServicesResponse, error)
ListServices lists all registered services by querying the service registry via the event bus.
Parameters:
- ctx: The context for the gRPC call.
- req: The request object (empty for now).
Returns:
- A response containing a list of registered services.
- An error if the operation fails or times out.
func (*RegistrationServer) RegisterService ¶
func (s *RegistrationServer) RegisterService(ctx context.Context, req *v1.RegisterServiceRequest) (*v1.RegisterServiceResponse, error)
RegisterService handles a gRPC request to register a new upstream service. It sends a registration request to the event bus and waits for a response from the registration worker. This process is asynchronous, allowing the server to remain responsive while the registration is in progress.
A correlation ID is used to match the request with the corresponding result from the worker. The method waits for the result, with a timeout, and returns the registration details, including any discovered tools.
Parameters:
- ctx: The context for the gRPC call.
- req: The request containing the configuration of the service to be registered.
Returns:
- A response with the registration status and discovered tools.
- An error if the registration fails or times out.
func (*RegistrationServer) RegisterTools ¶
func (s *RegistrationServer) RegisterTools(_ context.Context, _ *v1.RegisterToolsRequest) (*v1.RegisterToolsResponse, error)
RegisterTools is not yet implemented. It is intended to handle the registration of tools for a service.
Parameters:
- ctx: The context for the gRPC call.
- req: The request containing the tools to register.
Returns:
- A response indicating success or failure.
- An error (currently always Unimplemented).
func (*RegistrationServer) UnregisterService ¶
func (s *RegistrationServer) UnregisterService(_ context.Context, _ *v1.UnregisterServiceRequest) (*v1.UnregisterServiceResponse, error)
UnregisterService is not yet implemented. It is intended to handle the unregistration of a service.
Parameters:
- ctx: The context for the gRPC call.
- req: The request containing the service ID to unregister.
Returns:
- A response indicating success or failure.
- An error (currently always Unimplemented).
type Router ¶
type Router struct {
// contains filtered or unexported fields
}
Router is responsible for mapping MCP method names to their corresponding handler functions. It provides a simple mechanism for registering and retrieving handlers.
func NewRouter ¶
func NewRouter() *Router
NewRouter creates and returns a new, empty Router.
Returns:
- A pointer to a new Router.
func (*Router) GetHandler ¶
func (r *Router) GetHandler(method string) (MethodHandler, bool)
GetHandler retrieves the handler function for a given MCP method name.
Parameters:
- method: The name of the MCP method.
Returns:
- The handler function if found.
- A boolean indicating whether a handler was found.
func (*Router) Register ¶
func (r *Router) Register(method string, handler MethodHandler)
Register associates a handler function with a specific MCP method name. If a handler for the method already exists, it will be overwritten.
Parameters:
- method: The name of the MCP method (e.g., "tools/call").
- handler: The function that will handle the method call.
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
Server is the core of the MCP Any application. It orchestrates the handling of MCP (Model Context Protocol) requests by managing various components such as tools, prompts, resources, and services. It uses an internal router to delegate requests to the appropriate handlers and communicates with backend workers via an event bus.
func NewServer ¶
func NewServer( _ context.Context, toolManager tool.ManagerInterface, promptManager prompt.ManagerInterface, resourceManager resource.ManagerInterface, authManager *auth.Manager, serviceRegistry *serviceregistry.ServiceRegistry, bus *bus.Provider, debug bool, ) (*Server, error)
NewServer creates and initializes a new MCP Any Server. It sets up the necessary managers for tools, prompts, and resources, configures the router with handlers for standard MCP methods, and establishes middleware for request processing, such as routing and tool list filtering.
The server is initialized with all the necessary components for handling MCP requests and managing the lifecycle of tools, prompts, and resources.
Parameters:
- ctx: The application's root context.
- toolManager: Manages the lifecycle and access to tools.
- promptManager: Manages the lifecycle and access to prompts.
- resourceManager: Manages the lifecycle and access to resources.
- authManager: Handles authentication for incoming requests.
- serviceRegistry: Keeps track of all registered upstream services.
- bus: The event bus used for asynchronous communication between components.
Returns a new instance of the Server and an error if initialization fails.
func (*Server) AddServiceInfo ¶
func (s *Server) AddServiceInfo(serviceID string, info *tool.ServiceInfo)
AddServiceInfo adds information about a service to the tool manager.
func (*Server) AuthManager ¶
AuthManager returns the server's authentication manager, which is responsible for handling authentication for incoming requests.
func (*Server) ClearToolsForService ¶
ClearToolsForService removes all tools associated with a specific service.
func (*Server) GetPrompt ¶
func (s *Server) GetPrompt( ctx context.Context, req *mcp.GetPromptRequest, ) (*mcp.GetPromptResult, error)
GetPrompt handles the "prompts/get" MCP request. It retrieves a specific prompt by name from the PromptManager and executes it with the provided arguments, returning the result. If the prompt is not found, it returns a prompt.ErrPromptNotFound error.
Parameters:
- ctx: The context for the request.
- req: The "prompts/get" request from the client, containing the prompt name and arguments.
Returns the result of the prompt execution or an error if the prompt is not found or execution fails.
func (*Server) GetServiceInfo ¶
func (s *Server) GetServiceInfo(serviceID string) (*tool.ServiceInfo, bool)
GetServiceInfo retrieves information about a service by its ID.
func (*Server) ListPrompts ¶
func (s *Server) ListPrompts( _ context.Context, _ *mcp.ListPromptsRequest, ) (*mcp.ListPromptsResult, error)
ListPrompts handles the "prompts/list" MCP request. It retrieves the list of available prompts from the PromptManager, converts them to the MCP format, and returns them to the client.
Parameters:
- ctx: The context for the request.
- req: The "prompts/list" request from the client.
Returns a list of available prompts or an error if the retrieval fails.
func (*Server) ListResources ¶
func (s *Server) ListResources( _ context.Context, _ *mcp.ListResourcesRequest, ) (*mcp.ListResourcesResult, error)
ListResources handles the "resources/list" MCP request. It fetches the list of available resources from the ResourceManager, converts them to the MCP format, and returns them to the client.
Parameters:
- ctx: The context for the request.
- req: The "resources/list" request from the client.
Returns a list of available resources or an error if the retrieval fails.
func (*Server) PromptManager ¶
func (s *Server) PromptManager() prompt.ManagerInterface
PromptManager returns the server's prompt manager, which is responsible for managing the lifecycle and access to prompts.
func (*Server) ReadResource ¶
func (s *Server) ReadResource( ctx context.Context, req *mcp.ReadResourceRequest, ) (*mcp.ReadResourceResult, error)
ReadResource handles the "resources/read" MCP request. It retrieves a specific resource by its URI from the ResourceManager and returns its content. If the resource is not found, it returns a resource.ErrResourceNotFound error.
Parameters:
- ctx: The context for the request.
- req: The "resources/read" request from the client, containing the URI of the resource to be read.
Returns the content of the resource or an error if the resource is not found or reading fails.
func (*Server) ResourceManager ¶
func (s *Server) ResourceManager() resource.ManagerInterface
ResourceManager returns the server's resource manager, which is responsible for managing the lifecycle and access to resources.
func (*Server) Server ¶
Server returns the underlying *mcp.Server instance, which provides access to the core MCP server functionality. This can be used for advanced configurations or direct interaction with the MCP server.
func (*Server) ServiceRegistry ¶
func (s *Server) ServiceRegistry() *serviceregistry.ServiceRegistry
ServiceRegistry returns the server's service registry, which keeps track of all registered upstream services.
func (*Server) SetMCPServer ¶
func (s *Server) SetMCPServer(mcpServer tool.MCPServerProvider)
SetMCPServer sets the MCP server provider for the tool manager.
func (*Server) SetReloadFunc ¶
SetReloadFunc sets the function to be called when a configuration reload is triggered.
func (*Server) ToolManager ¶
func (s *Server) ToolManager() tool.ManagerInterface
ToolManager returns the server's tool manager, which is responsible for managing the lifecycle and access to tools.