Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.
Go agentagenticaichatbotdsh-pluginembeddingsevaluationgenerative-aigolangknowledge-basellmmulti-tenantollamaopenaiquestion-answeringragrerankingsemantic-searchvector-searchwiki
fix(mcp): 分离SSE连接生命周期与请求超时 (#4140)
诉说晚秋cf7aabeaca98
diff --git a/internal/mcp/client.go b/internal/mcp/client.goindex ebc7dde2e..a749e8828 100644--- a/internal/mcp/client.go+++ b/internal/mcp/client.go@@ -238,17 +238,25 @@ func NewMCPClient(config *ClientConfig) (MCPClient, error) {if config.Service.URL == nil || *config.Service.URL == "" {return nil, fmt.Errorf("URL is required for SSE transport")}+ // SSE keeps its GET response body open for the connection lifetime.+ // OAuth retains the bounded HTTP client; SSE bounds startup and RPCs+ // separately without putting an expiration on the event stream.+ sseHTTPClient := *httpClient+ sseHTTPClient.Timeout = 0options := []transport.ClientOption{- transport.WithHTTPClient(httpClient),+ transport.WithHTTPClient(&sseHTTPClient),transport.WithHeaders(headers),+ transport.WithEndpointTimeout(timeout),+ transport.WithResponseTimeout(timeout),}if useOAuth {options = append(options, transport.WithOAuth(oauthConfig))}- connection, err = transport.NewSSE(*config.Service.URL, options...)+ sse, err := transport.NewSSE(*config.Service.URL, options...)if err != nil {return nil, fmt.Errorf("failed to create SSE client: %w", err)}+ connection = &boundedSSEConnection{SSE: sse, timeout: timeout}case types.MCPTransportHTTPStreamable:if config.Service.URL == nil || *config.Service.URL == "" {return nil, fmt.Errorf("URL is required for HTTP Streamable transport")diff --git a/internal/mcp/client_sse_lifetime_test.go b/internal/mcp/client_sse_lifetime_test.gonew file mode 100644index 000000000..3ee6169fa--- /dev/null+++ b/internal/mcp/client_sse_lifetime_test.go@@ -0,0 +1,62 @@+package mcp++import (+ "context"+ "net/http/httptest"+ "sync/atomic"+ "testing"+ "time"++ "github.com/Tencent/WeKnora/internal/types"+ "github.com/Tencent/WeKnora/internal/utils"+ sdk "github.com/mark3labs/mcp-go/mcp"+ "github.com/mark3labs/mcp-go/server"+ "github.com/stretchr/testify/require"+)++func TestSSECallBudgetIsNotConnectionLifetime(t *testing.T) {+ utils.SetSSRFWhitelistFromRaw("127.0.0.1")+ t.Cleanup(utils.ResetSSRFWhitelistForTest)+ for _, age := range []time.Duration{0, 1500 * time.Millisecond} {+ t.Run(age.String(), func(t *testing.T) { testSSECallAfterIdle(t, age) })+ }+}++func testSSECallAfterIdle(t *testing.T, age time.Duration) {+ t.Helper()+ var executions atomic.Int32+ peer := server.NewMCPServer("lifetime-audit", "1")+ peer.AddTool(sdk.NewTool("slow", sdk.WithDescription("600 ms controlled read")),+ func(context.Context, sdk.CallToolRequest) (*sdk.CallToolResult, error) {+ executions.Add(1)+ time.Sleep(600 * time.Millisecond)+ return sdk.NewToolResultText("complete"), nil+ })+ httpServer := httptest.NewServer(server.NewSSEServer(peer))+ t.Cleanup(httpServer.Close)+ manager := NewMCPManager(nil)+ t.Cleanup(manager.Shutdown)+ url := httpServer.URL + "/sse"+ svc := &types.MCPService{+ ID: "lifetime", Name: "lifetime", Enabled: true, URL: &url,+ TransportType: types.MCPTransportSSE,+ AdvancedConfig: &types.MCPAdvancedConfig{Timeout: 2},+ }+ ctx, cancel := context.WithTimeout(t.Context(), 8*time.Second)+ defer cancel()+ connectedAt := time.Now()+ c, err := manager.GetOrCreateClient(ctx, svc)+ require.NoError(t, err)+ _, err = c.ListTools(ctx)+ require.NoError(t, err)+ time.Sleep(age)+ require.True(t, c.IsConnected(), "connection must be healthy before the call")+ callCtx, stop := context.WithTimeout(ctx, 2*time.Second)+ defer stop()+ start := time.Now()+ result, err := c.CallTool(callCtx, "slow", map[string]interface{}{})+ t.Logf("connection_age=%s call_duration=%s executions=%d context_error=%v call_error=%v",+ time.Since(connectedAt), time.Since(start), executions.Load(), callCtx.Err(), err)+ require.NoError(t, err, "a 600 ms tool must fit its fresh 2 s call budget regardless of connection age")+ require.Equal(t, "complete", result.Content[0].Text)+}diff --git a/internal/mcp/sse_connection.go b/internal/mcp/sse_connection.gonew file mode 100644index 000000000..b6c84c04c--- /dev/null+++ b/internal/mcp/sse_connection.go@@ -0,0 +1,65 @@+package mcp++import (+ "context"+ "sync"+ "time"++ "github.com/mark3labs/mcp-go/client/transport"+ sdk "github.com/mark3labs/mcp-go/mcp"+)++// boundedSSEConnection separates establishment and RPC budgets from the+// persistent event stream. Embedding SSE preserves its optional transport hooks.+type boundedSSEConnection struct {+ *transport.SSE+ timeout time.Duration+ mu sync.Mutex+ cancel context.CancelCauseFunc+}++func (c *boundedSSEConnection) Start(ctx context.Context) error {+ lifetime, cancel := context.WithCancelCause(ctx)+ c.mu.Lock()+ c.cancel = cancel+ c.mu.Unlock()+ // Unlike WithTimeout, this timer can be stopped without cancelling the+ // successful stream. It covers headers as well as the endpoint event.+ timer := time.AfterFunc(c.timeout, func() { cancel(context.DeadlineExceeded) })+ err := c.SSE.Start(lifetime)+ if !timer.Stop() {+ cancel(context.DeadlineExceeded)+ }+ if cause := context.Cause(lifetime); cause != nil {+ err = cause+ }+ if err != nil {+ cancel(err)+ }+ return err+}++func (c *boundedSSEConnection) SendRequest(+ ctx context.Context, request transport.JSONRPCRequest,+) (*transport.JSONRPCResponse, error) {+ ctx, cancel := context.WithTimeout(ctx, c.timeout)+ defer cancel()+ // Includes both POST acknowledgement and waiting for its SSE response.+ return c.SSE.SendRequest(ctx, request)+}++func (c *boundedSSEConnection) SendNotification(ctx context.Context, notification sdk.JSONRPCNotification) error {+ ctx, cancel := context.WithTimeout(ctx, c.timeout)+ defer cancel()+ return c.SSE.SendNotification(ctx, notification)+}++func (c *boundedSSEConnection) Close() error {+ c.mu.Lock()+ cancel := c.cancel+ c.mu.Unlock()+ if cancel != nil {+ cancel(context.Canceled)+ }+ return c.SSE.Close()+}diff --git a/internal/mcp/sse_connection_test.go b/internal/mcp/sse_connection_test.gonew file mode 100644index 000000000..aeb520806--- /dev/null+++ b/internal/mcp/sse_connection_test.go@@ -0,0 +1,136 @@+package mcp++import (+ "context"+ "fmt"+ "io"+ "net/http"+ "net/http/httptest"+ "testing"+ "time"++ "github.com/mark3labs/mcp-go/client/transport"+ sdk "github.com/mark3labs/mcp-go/mcp"+ "github.com/stretchr/testify/require"+)++const sseTestBudget = 150 * time.Millisecond++func newBoundedSSETestConnection(t *testing.T, handler http.HandlerFunc) *boundedSSEConnection {+ t.Helper()+ server := httptest.NewServer(handler)+ t.Cleanup(server.Close)+ sse, err := transport.NewSSE(server.URL, transport.WithHTTPClient(server.Client()))+ require.NoError(t, err)+ c := &boundedSSEConnection{SSE: sse, timeout: sseTestBudget}+ t.Cleanup(func() { require.NoError(t, c.Close()) })+ return c+}++func TestSSEStartupBudgetAndCancellation(t *testing.T) {+ for _, mode := range []string{"headers", "endpoint", "cancelled"} {+ t.Run(mode, func(t *testing.T) {+ started, closed := make(chan struct{}), make(chan struct{})+ c := newBoundedSSETestConnection(t, func(w http.ResponseWriter, r *http.Request) {+ close(started)+ defer close(closed)+ if mode == "endpoint" {+ w.Header().Set("Content-Type", "text/event-stream")+ w.WriteHeader(http.StatusOK)+ w.(http.Flusher).Flush()+ }+ <-r.Context().Done()+ })+ ctx, cancel := context.WithCancel(t.Context())+ defer cancel()+ done := make(chan error, 1)+ go func() { done <- c.Start(ctx) }()+ <-started+ want := context.DeadlineExceeded+ if mode == "cancelled" {+ cancel()+ want = context.Canceled+ }+ select {+ case err := <-done:+ require.ErrorIs(t, err, want)+ case <-time.After(time.Second):+ t.Fatal("SSE establishment did not stop")+ }+ select {+ case <-closed:+ case <-time.After(time.Second):+ t.Fatal("failed establishment left the HTTP request running")+ }+ })+ }+}++func TestSSERPCBudgetKeepsEventStreamAlive(t *testing.T) {+ for _, mode := range []string{"post_headers", "post_body", "event_response", "notification", "cancelled"} {+ t.Run(mode, func(t *testing.T) { testSSERPCBudget(t, mode) })+ }+}++func testSSERPCBudget(t *testing.T, mode string) {+ t.Helper()+ streamClosed := make(chan struct{})+ c := newBoundedSSETestConnection(t, func(w http.ResponseWriter, r *http.Request) {+ if r.Method == http.MethodGet {+ w.Header().Set("Content-Type", "text/event-stream")+ _, _ = fmt.Fprint(w, "event: endpoint\ndata: /message\n\n")+ w.(http.Flusher).Flush()+ <-r.Context().Done()+ close(streamClosed)+ return+ }+ _, _ = io.Copy(io.Discard, r.Body)+ if mode == "event_response" {+ w.WriteHeader(http.StatusAccepted)+ return+ }+ if mode == "post_body" {+ w.WriteHeader(http.StatusAccepted)+ w.(http.Flusher).Flush()+ }+ <-r.Context().Done()+ })+ require.NoError(t, c.Start(t.Context()))+ ctx, cancel := context.WithTimeout(t.Context(), time.Second)+ defer cancel()+ if mode == "cancelled" {+ cancel()+ }+ start := time.Now()+ err := sendSSETestOperation(ctx, c, mode)+ require.Error(t, err)+ require.Less(t, time.Since(start), 750*time.Millisecond)+ if mode != "cancelled" {+ require.NoError(t, ctx.Err(), "service budget must expire before the caller budget")+ } else {+ require.ErrorIs(t, err, context.Canceled)+ }+ select {+ case <-streamClosed:+ t.Fatal("an RPC timeout or cancellation must not close the event stream")+ default:+ }+ require.NoError(t, c.Close())+ select {+ case <-streamClosed:+ case <-time.After(time.Second):+ t.Fatal("explicit Close did not release the event stream")+ }+}++func sendSSETestOperation(ctx context.Context, c *boundedSSEConnection, mode string) error {+ if mode == "notification" {+ return c.SendNotification(ctx, sdk.JSONRPCNotification{+ JSONRPC: "2.0", Notification: sdk.Notification{Method: "notifications/initialized"},+ })+ }+ _, err := c.SendRequest(ctx, transport.JSONRPCRequest{+ JSONRPC: "2.0", ID: sdk.NewRequestId(1), Method: "tools/list",+ })+ return err+}