package server
import (
"context"
"github.com/mark3labs/mcp-go/mcp"
)
type OnRegisterSessionHookFunc func (ctx context .Context , session ClientSession )
type OnUnregisterSessionHookFunc func (ctx context .Context , session ClientSession )
type BeforeAnyHookFunc func (ctx context .Context , id any , method mcp .MCPMethod , message any )
type OnSuccessHookFunc func (ctx context .Context , id any , method mcp .MCPMethod , message any , result any )
type OnErrorHookFunc func (ctx context .Context , id any , method mcp .MCPMethod , message any , err error )
type OnRequestInitializationFunc func (ctx context .Context , id any , message any ) error
type OnBeforeInitializeFunc func (ctx context .Context , id any , message *mcp .InitializeRequest )
type OnAfterInitializeFunc func (ctx context .Context , id any , message *mcp .InitializeRequest , result *mcp .InitializeResult )
type OnBeforePingFunc func (ctx context .Context , id any , message *mcp .PingRequest )
type OnAfterPingFunc func (ctx context .Context , id any , message *mcp .PingRequest , result *mcp .EmptyResult )
type OnBeforeSetLevelFunc func (ctx context .Context , id any , message *mcp .SetLevelRequest )
type OnAfterSetLevelFunc func (ctx context .Context , id any , message *mcp .SetLevelRequest , result *mcp .EmptyResult )
type OnBeforeListResourcesFunc func (ctx context .Context , id any , message *mcp .ListResourcesRequest )
type OnAfterListResourcesFunc func (ctx context .Context , id any , message *mcp .ListResourcesRequest , result *mcp .ListResourcesResult )
type OnBeforeListResourceTemplatesFunc func (ctx context .Context , id any , message *mcp .ListResourceTemplatesRequest )
type OnAfterListResourceTemplatesFunc func (ctx context .Context , id any , message *mcp .ListResourceTemplatesRequest , result *mcp .ListResourceTemplatesResult )
type OnBeforeReadResourceFunc func (ctx context .Context , id any , message *mcp .ReadResourceRequest )
type OnAfterReadResourceFunc func (ctx context .Context , id any , message *mcp .ReadResourceRequest , result *mcp .ReadResourceResult )
type OnBeforeListPromptsFunc func (ctx context .Context , id any , message *mcp .ListPromptsRequest )
type OnAfterListPromptsFunc func (ctx context .Context , id any , message *mcp .ListPromptsRequest , result *mcp .ListPromptsResult )
type OnBeforeGetPromptFunc func (ctx context .Context , id any , message *mcp .GetPromptRequest )
type OnAfterGetPromptFunc func (ctx context .Context , id any , message *mcp .GetPromptRequest , result *mcp .GetPromptResult )
type OnBeforeListToolsFunc func (ctx context .Context , id any , message *mcp .ListToolsRequest )
type OnAfterListToolsFunc func (ctx context .Context , id any , message *mcp .ListToolsRequest , result *mcp .ListToolsResult )
type OnBeforeCallToolFunc func (ctx context .Context , id any , message *mcp .CallToolRequest )
type OnAfterCallToolFunc func (ctx context .Context , id any , message *mcp .CallToolRequest , result any )
type OnBeforeGetTaskFunc func (ctx context .Context , id any , message *mcp .GetTaskRequest )
type OnAfterGetTaskFunc func (ctx context .Context , id any , message *mcp .GetTaskRequest , result *mcp .GetTaskResult )
type OnBeforeListTasksFunc func (ctx context .Context , id any , message *mcp .ListTasksRequest )
type OnAfterListTasksFunc func (ctx context .Context , id any , message *mcp .ListTasksRequest , result *mcp .ListTasksResult )
type OnBeforeTaskResultFunc func (ctx context .Context , id any , message *mcp .TaskResultRequest )
type OnAfterTaskResultFunc func (ctx context .Context , id any , message *mcp .TaskResultRequest , result *mcp .TaskResultResult )
type OnBeforeCancelTaskFunc func (ctx context .Context , id any , message *mcp .CancelTaskRequest )
type OnAfterCancelTaskFunc func (ctx context .Context , id any , message *mcp .CancelTaskRequest , result *mcp .CancelTaskResult )
type OnBeforeCompleteFunc func (ctx context .Context , id any , message *mcp .CompleteRequest )
type OnAfterCompleteFunc func (ctx context .Context , id any , message *mcp .CompleteRequest , result *mcp .CompleteResult )
type Hooks struct {
OnRegisterSession []OnRegisterSessionHookFunc
OnUnregisterSession []OnUnregisterSessionHookFunc
OnBeforeAny []BeforeAnyHookFunc
OnSuccess []OnSuccessHookFunc
OnError []OnErrorHookFunc
OnRequestInitialization []OnRequestInitializationFunc
OnBeforeInitialize []OnBeforeInitializeFunc
OnAfterInitialize []OnAfterInitializeFunc
OnBeforePing []OnBeforePingFunc
OnAfterPing []OnAfterPingFunc
OnBeforeSetLevel []OnBeforeSetLevelFunc
OnAfterSetLevel []OnAfterSetLevelFunc
OnBeforeListResources []OnBeforeListResourcesFunc
OnAfterListResources []OnAfterListResourcesFunc
OnBeforeListResourceTemplates []OnBeforeListResourceTemplatesFunc
OnAfterListResourceTemplates []OnAfterListResourceTemplatesFunc
OnBeforeReadResource []OnBeforeReadResourceFunc
OnAfterReadResource []OnAfterReadResourceFunc
OnBeforeListPrompts []OnBeforeListPromptsFunc
OnAfterListPrompts []OnAfterListPromptsFunc
OnBeforeGetPrompt []OnBeforeGetPromptFunc
OnAfterGetPrompt []OnAfterGetPromptFunc
OnBeforeListTools []OnBeforeListToolsFunc
OnAfterListTools []OnAfterListToolsFunc
OnBeforeCallTool []OnBeforeCallToolFunc
OnAfterCallTool []OnAfterCallToolFunc
OnBeforeGetTask []OnBeforeGetTaskFunc
OnAfterGetTask []OnAfterGetTaskFunc
OnBeforeListTasks []OnBeforeListTasksFunc
OnAfterListTasks []OnAfterListTasksFunc
OnBeforeTaskResult []OnBeforeTaskResultFunc
OnAfterTaskResult []OnAfterTaskResultFunc
OnBeforeCancelTask []OnBeforeCancelTaskFunc
OnAfterCancelTask []OnAfterCancelTaskFunc
OnBeforeComplete []OnBeforeCompleteFunc
OnAfterComplete []OnAfterCompleteFunc
}
func (c *Hooks ) AddBeforeAny (hook BeforeAnyHookFunc ) {
c .OnBeforeAny = append (c .OnBeforeAny , hook )
}
func (c *Hooks ) AddOnSuccess (hook OnSuccessHookFunc ) {
c .OnSuccess = append (c .OnSuccess , hook )
}
func (c *Hooks ) AddOnError (hook OnErrorHookFunc ) {
c .OnError = append (c .OnError , hook )
}
func (c *Hooks ) beforeAny (ctx context .Context , id any , method mcp .MCPMethod , message any ) {
if c == nil {
return
}
for _ , hook := range c .OnBeforeAny {
hook (ctx , id , method , message )
}
}
func (c *Hooks ) onSuccess (ctx context .Context , id any , method mcp .MCPMethod , message any , result any ) {
if c == nil {
return
}
for _ , hook := range c .OnSuccess {
hook (ctx , id , method , message , result )
}
}
func (c *Hooks ) onError (ctx context .Context , id any , method mcp .MCPMethod , message any , err error ) {
if c == nil {
return
}
for _ , hook := range c .OnError {
hook (ctx , id , method , message , err )
}
}
func (c *Hooks ) AddOnRegisterSession (hook OnRegisterSessionHookFunc ) {
c .OnRegisterSession = append (c .OnRegisterSession , hook )
}
func (c *Hooks ) RegisterSession (ctx context .Context , session ClientSession ) {
if c == nil {
return
}
for _ , hook := range c .OnRegisterSession {
hook (ctx , session )
}
}
func (c *Hooks ) AddOnUnregisterSession (hook OnUnregisterSessionHookFunc ) {
c .OnUnregisterSession = append (c .OnUnregisterSession , hook )
}
func (c *Hooks ) UnregisterSession (ctx context .Context , session ClientSession ) {
if c == nil {
return
}
for _ , hook := range c .OnUnregisterSession {
hook (ctx , session )
}
}
func (c *Hooks ) AddOnRequestInitialization (hook OnRequestInitializationFunc ) {
c .OnRequestInitialization = append (c .OnRequestInitialization , hook )
}
func (c *Hooks ) onRequestInitialization (ctx context .Context , id any , message any ) error {
if c == nil {
return nil
}
for _ , hook := range c .OnRequestInitialization {
err := hook (ctx , id , message )
if err != nil {
return err
}
}
return nil
}
func (c *Hooks ) AddBeforeInitialize (hook OnBeforeInitializeFunc ) {
c .OnBeforeInitialize = append (c .OnBeforeInitialize , hook )
}
func (c *Hooks ) AddAfterInitialize (hook OnAfterInitializeFunc ) {
c .OnAfterInitialize = append (c .OnAfterInitialize , hook )
}
func (c *Hooks ) beforeInitialize (ctx context .Context , id any , message *mcp .InitializeRequest ) {
c .beforeAny (ctx , id , mcp .MethodInitialize , message )
if c == nil {
return
}
for _ , hook := range c .OnBeforeInitialize {
hook (ctx , id , message )
}
}
func (c *Hooks ) afterInitialize (ctx context .Context , id any , message *mcp .InitializeRequest , result *mcp .InitializeResult ) {
c .onSuccess (ctx , id , mcp .MethodInitialize , message , result )
if c == nil {
return
}
for _ , hook := range c .OnAfterInitialize {
hook (ctx , id , message , result )
}
}
func (c *Hooks ) AddBeforePing (hook OnBeforePingFunc ) {
c .OnBeforePing = append (c .OnBeforePing , hook )
}
func (c *Hooks ) AddAfterPing (hook OnAfterPingFunc ) {
c .OnAfterPing = append (c .OnAfterPing , hook )
}
func (c *Hooks ) beforePing (ctx context .Context , id any , message *mcp .PingRequest ) {
c .beforeAny (ctx , id , mcp .MethodPing , message )
if c == nil {
return
}
for _ , hook := range c .OnBeforePing {
hook (ctx , id , message )
}
}
func (c *Hooks ) afterPing (ctx context .Context , id any , message *mcp .PingRequest , result *mcp .EmptyResult ) {
c .onSuccess (ctx , id , mcp .MethodPing , message , result )
if c == nil {
return
}
for _ , hook := range c .OnAfterPing {
hook (ctx , id , message , result )
}
}
func (c *Hooks ) AddBeforeSetLevel (hook OnBeforeSetLevelFunc ) {
c .OnBeforeSetLevel = append (c .OnBeforeSetLevel , hook )
}
func (c *Hooks ) AddAfterSetLevel (hook OnAfterSetLevelFunc ) {
c .OnAfterSetLevel = append (c .OnAfterSetLevel , hook )
}
func (c *Hooks ) beforeSetLevel (ctx context .Context , id any , message *mcp .SetLevelRequest ) {
c .beforeAny (ctx , id , mcp .MethodSetLogLevel , message )
if c == nil {
return
}
for _ , hook := range c .OnBeforeSetLevel {
hook (ctx , id , message )
}
}
func (c *Hooks ) afterSetLevel (ctx context .Context , id any , message *mcp .SetLevelRequest , result *mcp .EmptyResult ) {
c .onSuccess (ctx , id , mcp .MethodSetLogLevel , message , result )
if c == nil {
return
}
for _ , hook := range c .OnAfterSetLevel {
hook (ctx , id , message , result )
}
}
func (c *Hooks ) AddBeforeListResources (hook OnBeforeListResourcesFunc ) {
c .OnBeforeListResources = append (c .OnBeforeListResources , hook )
}
func (c *Hooks ) AddAfterListResources (hook OnAfterListResourcesFunc ) {
c .OnAfterListResources = append (c .OnAfterListResources , hook )
}
func (c *Hooks ) beforeListResources (ctx context .Context , id any , message *mcp .ListResourcesRequest ) {
c .beforeAny (ctx , id , mcp .MethodResourcesList , message )
if c == nil {
return
}
for _ , hook := range c .OnBeforeListResources {
hook (ctx , id , message )
}
}
func (c *Hooks ) afterListResources (ctx context .Context , id any , message *mcp .ListResourcesRequest , result *mcp .ListResourcesResult ) {
c .onSuccess (ctx , id , mcp .MethodResourcesList , message , result )
if c == nil {
return
}
for _ , hook := range c .OnAfterListResources {
hook (ctx , id , message , result )
}
}
func (c *Hooks ) AddBeforeListResourceTemplates (hook OnBeforeListResourceTemplatesFunc ) {
c .OnBeforeListResourceTemplates = append (c .OnBeforeListResourceTemplates , hook )
}
func (c *Hooks ) AddAfterListResourceTemplates (hook OnAfterListResourceTemplatesFunc ) {
c .OnAfterListResourceTemplates = append (c .OnAfterListResourceTemplates , hook )
}
func (c *Hooks ) beforeListResourceTemplates (ctx context .Context , id any , message *mcp .ListResourceTemplatesRequest ) {
c .beforeAny (ctx , id , mcp .MethodResourcesTemplatesList , message )
if c == nil {
return
}
for _ , hook := range c .OnBeforeListResourceTemplates {
hook (ctx , id , message )
}
}
func (c *Hooks ) afterListResourceTemplates (ctx context .Context , id any , message *mcp .ListResourceTemplatesRequest , result *mcp .ListResourceTemplatesResult ) {
c .onSuccess (ctx , id , mcp .MethodResourcesTemplatesList , message , result )
if c == nil {
return
}
for _ , hook := range c .OnAfterListResourceTemplates {
hook (ctx , id , message , result )
}
}
func (c *Hooks ) AddBeforeReadResource (hook OnBeforeReadResourceFunc ) {
c .OnBeforeReadResource = append (c .OnBeforeReadResource , hook )
}
func (c *Hooks ) AddAfterReadResource (hook OnAfterReadResourceFunc ) {
c .OnAfterReadResource = append (c .OnAfterReadResource , hook )
}
func (c *Hooks ) beforeReadResource (ctx context .Context , id any , message *mcp .ReadResourceRequest ) {
c .beforeAny (ctx , id , mcp .MethodResourcesRead , message )
if c == nil {
return
}
for _ , hook := range c .OnBeforeReadResource {
hook (ctx , id , message )
}
}
func (c *Hooks ) afterReadResource (ctx context .Context , id any , message *mcp .ReadResourceRequest , result *mcp .ReadResourceResult ) {
c .onSuccess (ctx , id , mcp .MethodResourcesRead , message , result )
if c == nil {
return
}
for _ , hook := range c .OnAfterReadResource {
hook (ctx , id , message , result )
}
}
func (c *Hooks ) AddBeforeListPrompts (hook OnBeforeListPromptsFunc ) {
c .OnBeforeListPrompts = append (c .OnBeforeListPrompts , hook )
}
func (c *Hooks ) AddAfterListPrompts (hook OnAfterListPromptsFunc ) {
c .OnAfterListPrompts = append (c .OnAfterListPrompts , hook )
}
func (c *Hooks ) beforeListPrompts (ctx context .Context , id any , message *mcp .ListPromptsRequest ) {
c .beforeAny (ctx , id , mcp .MethodPromptsList , message )
if c == nil {
return
}
for _ , hook := range c .OnBeforeListPrompts {
hook (ctx , id , message )
}
}
func (c *Hooks ) afterListPrompts (ctx context .Context , id any , message *mcp .ListPromptsRequest , result *mcp .ListPromptsResult ) {
c .onSuccess (ctx , id , mcp .MethodPromptsList , message , result )
if c == nil {
return
}
for _ , hook := range c .OnAfterListPrompts {
hook (ctx , id , message , result )
}
}
func (c *Hooks ) AddBeforeGetPrompt (hook OnBeforeGetPromptFunc ) {
c .OnBeforeGetPrompt = append (c .OnBeforeGetPrompt , hook )
}
func (c *Hooks ) AddAfterGetPrompt (hook OnAfterGetPromptFunc ) {
c .OnAfterGetPrompt = append (c .OnAfterGetPrompt , hook )
}
func (c *Hooks ) beforeGetPrompt (ctx context .Context , id any , message *mcp .GetPromptRequest ) {
c .beforeAny (ctx , id , mcp .MethodPromptsGet , message )
if c == nil {
return
}
for _ , hook := range c .OnBeforeGetPrompt {
hook (ctx , id , message )
}
}
func (c *Hooks ) afterGetPrompt (ctx context .Context , id any , message *mcp .GetPromptRequest , result *mcp .GetPromptResult ) {
c .onSuccess (ctx , id , mcp .MethodPromptsGet , message , result )
if c == nil {
return
}
for _ , hook := range c .OnAfterGetPrompt {
hook (ctx , id , message , result )
}
}
func (c *Hooks ) AddBeforeListTools (hook OnBeforeListToolsFunc ) {
c .OnBeforeListTools = append (c .OnBeforeListTools , hook )
}
func (c *Hooks ) AddAfterListTools (hook OnAfterListToolsFunc ) {
c .OnAfterListTools = append (c .OnAfterListTools , hook )
}
func (c *Hooks ) beforeListTools (ctx context .Context , id any , message *mcp .ListToolsRequest ) {
c .beforeAny (ctx , id , mcp .MethodToolsList , message )
if c == nil {
return
}
for _ , hook := range c .OnBeforeListTools {
hook (ctx , id , message )
}
}
func (c *Hooks ) afterListTools (ctx context .Context , id any , message *mcp .ListToolsRequest , result *mcp .ListToolsResult ) {
c .onSuccess (ctx , id , mcp .MethodToolsList , message , result )
if c == nil {
return
}
for _ , hook := range c .OnAfterListTools {
hook (ctx , id , message , result )
}
}
func (c *Hooks ) AddBeforeCallTool (hook OnBeforeCallToolFunc ) {
c .OnBeforeCallTool = append (c .OnBeforeCallTool , hook )
}
func (c *Hooks ) AddAfterCallTool (hook OnAfterCallToolFunc ) {
c .OnAfterCallTool = append (c .OnAfterCallTool , hook )
}
func (c *Hooks ) beforeCallTool (ctx context .Context , id any , message *mcp .CallToolRequest ) {
c .beforeAny (ctx , id , mcp .MethodToolsCall , message )
if c == nil {
return
}
for _ , hook := range c .OnBeforeCallTool {
hook (ctx , id , message )
}
}
func (c *Hooks ) afterCallTool (ctx context .Context , id any , message *mcp .CallToolRequest , result any ) {
c .onSuccess (ctx , id , mcp .MethodToolsCall , message , result )
if c == nil {
return
}
for _ , hook := range c .OnAfterCallTool {
hook (ctx , id , message , result )
}
}
func (c *Hooks ) AddBeforeGetTask (hook OnBeforeGetTaskFunc ) {
c .OnBeforeGetTask = append (c .OnBeforeGetTask , hook )
}
func (c *Hooks ) AddAfterGetTask (hook OnAfterGetTaskFunc ) {
c .OnAfterGetTask = append (c .OnAfterGetTask , hook )
}
func (c *Hooks ) beforeGetTask (ctx context .Context , id any , message *mcp .GetTaskRequest ) {
c .beforeAny (ctx , id , mcp .MethodTasksGet , message )
if c == nil {
return
}
for _ , hook := range c .OnBeforeGetTask {
hook (ctx , id , message )
}
}
func (c *Hooks ) afterGetTask (ctx context .Context , id any , message *mcp .GetTaskRequest , result *mcp .GetTaskResult ) {
c .onSuccess (ctx , id , mcp .MethodTasksGet , message , result )
if c == nil {
return
}
for _ , hook := range c .OnAfterGetTask {
hook (ctx , id , message , result )
}
}
func (c *Hooks ) AddBeforeListTasks (hook OnBeforeListTasksFunc ) {
c .OnBeforeListTasks = append (c .OnBeforeListTasks , hook )
}
func (c *Hooks ) AddAfterListTasks (hook OnAfterListTasksFunc ) {
c .OnAfterListTasks = append (c .OnAfterListTasks , hook )
}
func (c *Hooks ) beforeListTasks (ctx context .Context , id any , message *mcp .ListTasksRequest ) {
c .beforeAny (ctx , id , mcp .MethodTasksList , message )
if c == nil {
return
}
for _ , hook := range c .OnBeforeListTasks {
hook (ctx , id , message )
}
}
func (c *Hooks ) afterListTasks (ctx context .Context , id any , message *mcp .ListTasksRequest , result *mcp .ListTasksResult ) {
c .onSuccess (ctx , id , mcp .MethodTasksList , message , result )
if c == nil {
return
}
for _ , hook := range c .OnAfterListTasks {
hook (ctx , id , message , result )
}
}
func (c *Hooks ) AddBeforeTaskResult (hook OnBeforeTaskResultFunc ) {
c .OnBeforeTaskResult = append (c .OnBeforeTaskResult , hook )
}
func (c *Hooks ) AddAfterTaskResult (hook OnAfterTaskResultFunc ) {
c .OnAfterTaskResult = append (c .OnAfterTaskResult , hook )
}
func (c *Hooks ) beforeTaskResult (ctx context .Context , id any , message *mcp .TaskResultRequest ) {
c .beforeAny (ctx , id , mcp .MethodTasksResult , message )
if c == nil {
return
}
for _ , hook := range c .OnBeforeTaskResult {
hook (ctx , id , message )
}
}
func (c *Hooks ) afterTaskResult (ctx context .Context , id any , message *mcp .TaskResultRequest , result *mcp .TaskResultResult ) {
c .onSuccess (ctx , id , mcp .MethodTasksResult , message , result )
if c == nil {
return
}
for _ , hook := range c .OnAfterTaskResult {
hook (ctx , id , message , result )
}
}
func (c *Hooks ) AddBeforeCancelTask (hook OnBeforeCancelTaskFunc ) {
c .OnBeforeCancelTask = append (c .OnBeforeCancelTask , hook )
}
func (c *Hooks ) AddAfterCancelTask (hook OnAfterCancelTaskFunc ) {
c .OnAfterCancelTask = append (c .OnAfterCancelTask , hook )
}
func (c *Hooks ) beforeCancelTask (ctx context .Context , id any , message *mcp .CancelTaskRequest ) {
c .beforeAny (ctx , id , mcp .MethodTasksCancel , message )
if c == nil {
return
}
for _ , hook := range c .OnBeforeCancelTask {
hook (ctx , id , message )
}
}
func (c *Hooks ) afterCancelTask (ctx context .Context , id any , message *mcp .CancelTaskRequest , result *mcp .CancelTaskResult ) {
c .onSuccess (ctx , id , mcp .MethodTasksCancel , message , result )
if c == nil {
return
}
for _ , hook := range c .OnAfterCancelTask {
hook (ctx , id , message , result )
}
}
func (c *Hooks ) AddBeforeComplete (hook OnBeforeCompleteFunc ) {
c .OnBeforeComplete = append (c .OnBeforeComplete , hook )
}
func (c *Hooks ) AddAfterComplete (hook OnAfterCompleteFunc ) {
c .OnAfterComplete = append (c .OnAfterComplete , hook )
}
func (c *Hooks ) beforeComplete (ctx context .Context , id any , message *mcp .CompleteRequest ) {
c .beforeAny (ctx , id , mcp .MethodCompletionComplete , message )
if c == nil {
return
}
for _ , hook := range c .OnBeforeComplete {
hook (ctx , id , message )
}
}
func (c *Hooks ) afterComplete (ctx context .Context , id any , message *mcp .CompleteRequest , result *mcp .CompleteResult ) {
c .onSuccess (ctx , id , mcp .MethodCompletionComplete , message , result )
if c == nil {
return
}
for _ , hook := range c .OnAfterComplete {
hook (ctx , id , message , result )
}
}
The pages are generated with Golds v0.8.4 . (GOOS=linux GOARCH=amd64)
Golds is a Go 101 project developed by Tapir Liu .
PR and bug reports are welcome and can be submitted to the issue list .
Please follow @zigo_101 (reachable from the left QR code) to get the latest news of Golds .