mcpserver

package
v0.0.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 19, 2025 License: Apache-2.0 Imports: 20 Imported by: 0

Documentation

Overview

Package mcpserver implements the MCP server functionality.

Index

Constants

This section is empty.

Variables

View Source
var AddReceivingMiddlewareHook func(name string)

AddReceivingMiddlewareHook is a hook for adding receiving middleware.

View Source
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

type MethodHandler func(ctx context.Context, req mcp.Request) (mcp.Result, error)

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

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

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

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

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

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

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) AddTool

func (s *Server) AddTool(t tool.Tool) error

AddTool registers a new tool with the tool manager.

func (*Server) AuthManager

func (s *Server) AuthManager() *auth.Manager

AuthManager returns the server's authentication manager, which is responsible for handling authentication for incoming requests.

func (*Server) CallTool

func (s *Server) CallTool(ctx context.Context, req *tool.ExecutionRequest) (any, error)

CallTool executes a tool with the provided request.

func (*Server) ClearToolsForService

func (s *Server) ClearToolsForService(serviceKey string)

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) GetTool

func (s *Server) GetTool(toolName string) (tool.Tool, bool)

GetTool retrieves a tool by its name.

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) ListTools

func (s *Server) ListTools() []tool.Tool

ListTools returns a list of all available tools.

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) Reload

func (s *Server) Reload() error

Reload reloads the server's configuration and updates its state.

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

func (s *Server) Server() *mcp.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

func (s *Server) SetReloadFunc(f func() error)

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL