Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@ func RateLimitMiddleware(limiter pkg.RateLimiter) ToolMiddleware {
}
}

// TimeoutMiddleware returns a middleware that enforces a per-tool execution timeout.
// If the tool handler does not complete within the given duration, the context is
// canceled and a timeout error is returned. This prevents slow or hanging tools
// from blocking the MCP server.
//
// Example:
//
// server.RegisterTool(&protocol.Tool{Name: "search"},
// server.TimeoutMiddleware(30*time.Second)(myHandler),
// )
func TimeoutMiddleware(timeout time.Duration) ToolMiddleware {
return func(next ToolHandlerFunc) ToolHandlerFunc {
return func(ctx context.Context, req *protocol.CallToolRequest) (*protocol.CallToolResult, error) {
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
return next(ctx, req)
}
}
}

func WithPagination(limit int) Option {
return func(s *Server) {
s.paginationLimit = limit
Expand Down
Loading