Documentation
¶
Overview ¶
Package mcp implements Dynamic Client Registration Protocol (RFC 7591)
Package mcp provides a client for the Model Control Protocol (MCP) that allows the AI plugin to access external tools provided by MCP servers.
The UserClients represents a single user's connection to multiple MCP servers. The Client represents a connection to a single MCP server. The UserClients currently only supports authentication via Mattermost user ID header X-Mattermost-UserID. In the future it will support our OAuth implementation.
The ClientManager manages multiple UserClients, allowing for efficient mangement of connections. It is responsible for creating and closing UserClients as needed.
The organization reflects the need for each user to have their own connection to the MCP server given the design of MCP.
Index ¶
- Constants
- func GetRegistrationEndpoint(ctx context.Context, httpClient *http.Client, serverURL string) (string, error)
- type AuthorizationServerMetadata
- type Client
- type ClientCredentials
- type ClientManager
- func (m *ClientManager) Close()
- func (m *ClientManager) GetOAuthManager() *OAuthManager
- func (m *ClientManager) GetToolsForUser(userID string) ([]llm.Tool, *Errors)
- func (m *ClientManager) ProcessOAuthCallback(ctx context.Context, userID, state, code string) (*OAuthSession, error)
- func (m *ClientManager) ReInit(config Config)
- type Config
- type Errors
- type OAuthManager
- type OAuthNeededError
- type OAuthSession
- type ProtectedResourceMetadata
- type RegistrationError
- type RegistrationRequest
- type RegistrationResponse
- type ServerConfig
- type ToolInfo
- type UserClients
Constants ¶
const MMUserIDHeader = "X-Mattermost-UserID"
Variables ¶
This section is empty.
Functions ¶
Types ¶
type AuthorizationServerMetadata ¶ added in v1.4.0
type AuthorizationServerMetadata struct {
Issuer string `json:"issuer"`
AuthorizationEndpoint string `json:"authorization_endpoint"`
TokenEndpoint string `json:"token_endpoint"`
ResponseTypesSupported []string `json:"response_types_supported"`
GrantTypesSupported []string `json:"grant_types_supported,omitempty"`
ScopesSupported []string `json:"scopes_supported,omitempty"`
RegistrationEndpoint string `json:"registration_endpoint,omitempty"`
}
AuthorizationServerMetadata represents the OAuth 2.0 Authorization Server Metadata (RFC 8414)
type Client ¶ added in v1.4.0
type Client struct {
// contains filtered or unexported fields
}
Client represents the connection to a single MCP server
func NewClient ¶ added in v1.4.0
func NewClient(ctx context.Context, userID string, serverConfig ServerConfig, log pluginapi.LogService, oauthManager *OAuthManager) (*Client, error)
NewClient creates a new MCP client for the given server and user and connects to the specified MCP server
func (*Client) CallTool ¶ added in v1.4.0
func (c *Client) CallTool(ctx context.Context, toolName string, args map[string]any) (string, error)
CallTool calls a tool on this MCP server
type ClientCredentials ¶ added in v1.4.0
type ClientManager ¶
type ClientManager struct {
// contains filtered or unexported fields
}
ClientManager manages MCP clients for multiple users
func NewClientManager ¶
func NewClientManager(config Config, log pluginapi.LogService, pluginAPI *pluginapi.Client, oauthManager *OAuthManager) *ClientManager
NewClientManager creates a new MCP client manager
func (*ClientManager) Close ¶
func (m *ClientManager) Close()
Close closes the client manager and all managed clients The client manger should not be used after Close is called
func (*ClientManager) GetOAuthManager ¶ added in v1.4.0
func (m *ClientManager) GetOAuthManager() *OAuthManager
GetOAuthManager returns the OAuth manager instance
func (*ClientManager) GetToolsForUser ¶
func (m *ClientManager) GetToolsForUser(userID string) ([]llm.Tool, *Errors)
GetToolsForUser returns the tools available for a specific user
func (*ClientManager) ProcessOAuthCallback ¶ added in v1.4.0
func (m *ClientManager) ProcessOAuthCallback(ctx context.Context, userID, state, code string) (*OAuthSession, error)
ProcessOAuthCallback processes the OAuth callback for a user
func (*ClientManager) ReInit ¶
func (m *ClientManager) ReInit(config Config)
ReInit re-initializes the client manager with a new configuration
type Config ¶
type Config struct {
Enabled bool `json:"enabled"`
Servers []ServerConfig `json:"servers"`
IdleTimeoutMinutes int `json:"idleTimeoutMinutes"`
}
Config contains the configuration for the MCP servers
type Errors ¶ added in v1.4.0
type Errors struct {
ToolAuthErrors []llm.ToolAuthError // Authentication errors users need to resolve
Errors []error // Generic errors (connection, config, etc.)
}
Errors represents a collection of errors from MCP operations.
type OAuthManager ¶ added in v1.4.0
type OAuthManager struct {
// contains filtered or unexported fields
}
func NewOAuthManager ¶ added in v1.4.0
func NewOAuthManager(pluginAPI mmapi.Client, callbackURL string) *OAuthManager
func (*OAuthManager) InitiateOAuthFlow ¶ added in v1.4.0
func (*OAuthManager) ProcessCallback ¶ added in v1.4.0
func (m *OAuthManager) ProcessCallback(ctx context.Context, loggedInUserID, state, code string) (*OAuthSession, error)
type OAuthNeededError ¶ added in v1.4.0
type OAuthNeededError struct {
// contains filtered or unexported fields
}
func (*OAuthNeededError) AuthURL ¶ added in v1.4.0
func (e *OAuthNeededError) AuthURL() string
func (*OAuthNeededError) Error ¶ added in v1.4.0
func (e *OAuthNeededError) Error() string
func (*OAuthNeededError) Unwrap ¶ added in v1.4.0
func (e *OAuthNeededError) Unwrap() error
type OAuthSession ¶ added in v1.4.0
type ProtectedResourceMetadata ¶ added in v1.4.0
type ProtectedResourceMetadata struct {
Resource string `json:"resource"`
AuthorizationServers []string `json:"authorization_servers"`
}
ProtectedResourceMetadata represents the OAuth 2.0 Protected Resource Metadata (RFC 9728)
type RegistrationError ¶ added in v1.4.0
type RegistrationError struct {
ErrorCode string `json:"error"`
ErrorDescription string `json:"error_description,omitempty"`
HTTPStatusCode int `json:"-"`
HTTPResponse *http.Response `json:"-"`
}
RegistrationError represents an error response per RFC 7591
func (*RegistrationError) Error ¶ added in v1.4.0
func (e *RegistrationError) Error() string
type RegistrationRequest ¶ added in v1.4.0
type RegistrationRequest struct {
// Required fields
RedirectURIs []string `json:"redirect_uris"`
// Optional fields commonly used
TokenEndpointAuthMethod string `json:"token_endpoint_auth_method,omitempty"`
GrantTypes []string `json:"grant_types,omitempty"`
ResponseTypes []string `json:"response_types,omitempty"`
ClientName string `json:"client_name,omitempty"`
Scope string `json:"scope,omitempty"`
Contacts []string `json:"contacts,omitempty"`
// Additional optional fields can be added as needed
ClientURI string `json:"client_uri,omitempty"`
LogoURI string `json:"logo_uri,omitempty"`
ToSURI string `json:"tos_uri,omitempty"`
PolicyURI string `json:"policy_uri,omitempty"`
}
RegistrationRequest represents a client registration request per RFC 7591
func DefaultRegistrationRequest ¶ added in v1.4.0
func DefaultRegistrationRequest(redirectURI, clientName string) *RegistrationRequest
DefaultRegistrationRequest creates a default registration request for MCP clients
type RegistrationResponse ¶ added in v1.4.0
type RegistrationResponse struct {
// Required fields
ClientID string `json:"client_id"`
// Optional fields
ClientSecret string `json:"client_secret,omitempty"`
ClientIDIssuedAt *int64 `json:"client_id_issued_at,omitempty"`
ClientSecretExpiresAt *int64 `json:"client_secret_expires_at,omitempty"`
// Echo back the registration metadata
RedirectURIs []string `json:"redirect_uris,omitempty"`
TokenEndpointAuthMethod string `json:"token_endpoint_auth_method,omitempty"`
GrantTypes []string `json:"grant_types,omitempty"`
ResponseTypes []string `json:"response_types,omitempty"`
ClientName string `json:"client_name,omitempty"`
Scope string `json:"scope,omitempty"`
Contacts []string `json:"contacts,omitempty"`
ClientURI string `json:"client_uri,omitempty"`
LogoURI string `json:"logo_uri,omitempty"`
ToSURI string `json:"tos_uri,omitempty"`
PolicyURI string `json:"policy_uri,omitempty"`
}
RegistrationResponse represents the server's response per RFC 7591
func DiscoverAndRegisterClient ¶ added in v1.4.0
func DiscoverAndRegisterClient(ctx context.Context, httpClient *http.Client, serverURL, callbackURL, clientID, initialAccessToken string) (*RegistrationResponse, error)
DiscoverAndRegisterClient performs the complete client registration flow: 1. Discovers the registration endpoint from server metadata 2. Creates a default registration request 3. Registers the client with the server
func RegisterClient ¶ added in v1.4.0
func RegisterClient(ctx context.Context, httpClient *http.Client, registrationEndpoint string, request *RegistrationRequest, initialAccessToken string) (*RegistrationResponse, error)
RegisterClient performs dynamic client registration per RFC 7591
type ServerConfig ¶
type ServerConfig struct {
Name string `json:"name"`
Enabled bool `json:"enabled"`
BaseURL string `json:"baseURL"`
Headers map[string]string `json:"headers,omitempty"`
}
ServerConfig contains the configuration for a single MCP server
type ToolInfo ¶ added in v1.4.0
type ToolInfo struct {
Name string `json:"name"`
Description string `json:"description"`
InputSchema map[string]interface{} `json:"inputSchema"`
}
ToolInfo represents a tool's metadata for discovery purposes
func DiscoverServerTools ¶ added in v1.4.0
func DiscoverServerTools( ctx context.Context, userID string, serverConfig ServerConfig, log pluginapi.LogService, oauthManger *OAuthManager, ) ([]ToolInfo, error)
DiscoverServerTools creates a temporary connection to an MCP server and discovers its tools
type UserClients ¶ added in v1.4.0
type UserClients struct {
// contains filtered or unexported fields
}
UserClients represents a per-user MCP client with multiple server connections
func NewUserClients ¶ added in v1.4.0
func NewUserClients(userID string, log pluginapi.LogService, oauthManager *OAuthManager) *UserClients
func (*UserClients) Close ¶ added in v1.4.0
func (c *UserClients) Close()
Close closes all server connections for a user client
func (*UserClients) ConnectToAllServers ¶ added in v1.4.0
func (c *UserClients) ConnectToAllServers(servers []ServerConfig) *Errors
ConnectToAllServers initializes connections to all provided servers
func (*UserClients) GetTools ¶ added in v1.4.0
func (c *UserClients) GetTools() []llm.Tool
GetTools returns the tools available from the clients