Skip to content
Open
Show file tree
Hide file tree
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
15 changes: 11 additions & 4 deletions e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/mark3labs/mcp-go/mcp"
"github.com/stretchr/testify/require"

sdk "github.com/flashcatcloud/flashduty-sdk"
"github.com/flashcatcloud/flashduty-mcp-server/internal/flashduty"
pkgflashduty "github.com/flashcatcloud/flashduty-mcp-server/pkg/flashduty"
"github.com/flashcatcloud/flashduty-mcp-server/pkg/translations"
Expand Down Expand Up @@ -55,13 +56,19 @@ func getE2EBaseURL() string {
return baseURL
}

// getAPIClient creates a native Flashduty API client for verification purposes
func getAPIClient(t *testing.T) *pkgflashduty.Client {
// getAPIClient creates a native Flashduty SDK client for verification purposes
func getAPIClient(t *testing.T) *sdk.Client {
appKey := getE2EAppKey(t)
baseURL := getE2EBaseURL()

client, err := pkgflashduty.NewClient(appKey, baseURL, "e2e-test-client/1.0.0")
require.NoError(t, err, "expected to create Flashduty API client")
opts := []sdk.Option{
sdk.WithUserAgent("e2e-test-client/1.0.0"),
}
if baseURL != "" {
opts = append(opts, sdk.WithBaseURL(baseURL))
}
client, err := sdk.NewClient(appKey, opts...)
require.NoError(t, err, "expected to create Flashduty SDK client")

return client
}
Expand Down
5 changes: 3 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,13 @@ go 1.24.4

require (
github.com/bluele/gcache v0.0.2
github.com/flashcatcloud/flashduty-sdk v0.3.1
github.com/google/go-github/v72 v72.0.0
github.com/josephburnett/jd v1.9.2
github.com/mark3labs/mcp-go v0.45.0
github.com/spf13/cobra v1.10.2
github.com/spf13/viper v1.20.1
github.com/stretchr/testify v1.10.0
github.com/toon-format/toon-go v0.0.0-20251202084852-7ca0e27c4e8c
golang.org/x/sync v0.19.0
)

require (
Expand All @@ -24,10 +23,12 @@ require (
github.com/josharian/intern v1.0.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/rogpeppe/go-internal v1.13.1 // indirect
github.com/toon-format/toon-go v0.0.0-20251202084852-7ca0e27c4e8c // indirect
github.com/wk8/go-ordered-map/v2 v2.1.8 // indirect
github.com/yosida95/uritemplate/v3 v3.0.2 // indirect
github.com/yudai/golcs v0.0.0-20170316035057-ecda9a501e82 // indirect
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56 // indirect
golang.org/x/sync v0.19.0 // indirect
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/flashcatcloud/flashduty-sdk v0.3.1 h1:60JHV22kauE4fOfUw7Yv2/XcbAMSmRFiJJc+CBl/dsU=
github.com/flashcatcloud/flashduty-sdk v0.3.1/go.mod h1:dG4eJfdZaj4jNBMwEexbfK/3PmcIMhNeJ88L/DcZzUY=
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/frankban/quicktest v1.14.6/go.mod h1:4ptaffx2x8+WTWXmUCuVU6aPUX1/Mz7zb5vbUoiM6w0=
github.com/fsnotify/fsnotify v1.8.0 h1:dAwr6QBTBZIkG8roQaJjGof0pp0EeF+tNV7YBP3F/8M=
Expand Down
30 changes: 22 additions & 8 deletions internal/flashduty/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package flashduty
import (
"context"
"fmt"
"net/http"
"time"

"github.com/bluele/gcache"

"github.com/flashcatcloud/flashduty-mcp-server/pkg/flashduty"
"github.com/flashcatcloud/flashduty-mcp-server/pkg/trace"
sdk "github.com/flashcatcloud/flashduty-sdk"
)

type contextKey string
Expand All @@ -29,25 +31,25 @@ func ConfigFromContext(ctx context.Context) (FlashdutyConfig, bool) {
}

// clientFromContext returns the Flashduty client from the context.
func clientFromContext(ctx context.Context) (*flashduty.Client, bool) {
client, ok := ctx.Value(flashdutyClientKey).(*flashduty.Client)
func clientFromContext(ctx context.Context) (*sdk.Client, bool) {
client, ok := ctx.Value(flashdutyClientKey).(*sdk.Client)
return client, ok
}

// contextWithClient adds the Flashduty client to the context.
func contextWithClient(ctx context.Context, client *flashduty.Client) context.Context {
func contextWithClient(ctx context.Context, client *sdk.Client) context.Context {
return context.WithValue(ctx, flashdutyClientKey, client)
}

var clientCache = gcache.New(1000).
Expiration(time.Hour).
Build()

// getClientFromContext is a helper function for tool handlers to get a flashduty client.
// getClient is a helper function for tool handlers to get a flashduty client.
// It will try to get the client from the context first. If not found, it will create a new one
// based on the config in the context, and cache it in the context for future use in the same request.
// It falls back to the default config if no config is found in the context.
func getClient(ctx context.Context, defaultCfg FlashdutyConfig, version string) (context.Context, *flashduty.Client, error) {
func getClient(ctx context.Context, defaultCfg FlashdutyConfig, version string) (context.Context, *sdk.Client, error) {
if client, ok := clientFromContext(ctx); ok {
return ctx, client, nil
}
Expand All @@ -64,12 +66,24 @@ func getClient(ctx context.Context, defaultCfg FlashdutyConfig, version string)
// Use APP key and BaseURL as cache key to handle different environments.
cacheKey := fmt.Sprintf("%s|%s", cfg.APPKey, cfg.BaseURL)
if client, err := clientCache.Get(cacheKey); err == nil {
return contextWithClient(ctx, client.(*flashduty.Client)), client.(*flashduty.Client), nil
return contextWithClient(ctx, client.(*sdk.Client)), client.(*sdk.Client), nil
}

userAgent := fmt.Sprintf("flashduty-mcp-server/%s", version)

client, err := flashduty.NewClient(cfg.APPKey, cfg.BaseURL, userAgent)
opts := []sdk.Option{
sdk.WithUserAgent(userAgent),
sdk.WithRequestHook(func(req *http.Request) {
if traceCtx := trace.FromContext(req.Context()); traceCtx != nil {
traceCtx.SetHTTPHeaders(req.Header)
}
}),
}
if cfg.BaseURL != "" {
opts = append(opts, sdk.WithBaseURL(cfg.BaseURL))
}

client, err := sdk.NewClient(cfg.APPKey, opts...)
if err != nil {
return ctx, nil, fmt.Errorf("failed to create Flashduty client: %w", err)
}
Expand Down
50 changes: 26 additions & 24 deletions internal/flashduty/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"github.com/mark3labs/mcp-go/mcp"
"github.com/mark3labs/mcp-go/server"

sdk "github.com/flashcatcloud/flashduty-sdk"

pkgerrors "github.com/flashcatcloud/flashduty-mcp-server/pkg/errors"
"github.com/flashcatcloud/flashduty-mcp-server/pkg/flashduty"
mcplog "github.com/flashcatcloud/flashduty-mcp-server/pkg/log"
Expand Down Expand Up @@ -128,7 +130,7 @@ func NewMCPServer(cfg FlashdutyConfig) (*server.MCPServer, error) {

flashdutyServer := server.NewMCPServer("flashduty-mcp-server", cfg.Version, server.WithHooks(hooks))

getClientFn := func(ctx context.Context) (context.Context, *flashduty.Client, error) {
getClientFn := func(ctx context.Context) (context.Context, *sdk.Client, error) {
return getClient(ctx, cfg, cfg.Version)
}

Expand All @@ -145,6 +147,14 @@ func NewMCPServer(cfg FlashdutyConfig) (*server.MCPServer, error) {
return flashdutyServer, nil
}

func newStreamableHTTPServer(mcpServer *server.MCPServer, logger *slog.Logger, contextFunc server.HTTPContextFunc) *server.StreamableHTTPServer {
return server.NewStreamableHTTPServer(
mcpServer,
server.WithLogger(&slogAdapter{logger: logger}),
server.WithHTTPContextFunc(contextFunc),
)
}

type StdioServerConfig struct {
// Version of the server
Version string
Expand Down Expand Up @@ -337,29 +347,21 @@ func RunHTTPServer(cfg HTTPServerConfig) error {
return fmt.Errorf("failed to create MCP server: %w", err)
}

httpServer := server.NewStreamableHTTPServer(
mcpServer,
server.WithLogger(&slogAdapter{logger: logger}),
// Return 405 for GET requests — this server doesn't use server-initiated
// features (sampling, elicitation). Without this, the SDK's standalone SSE
// GET hangs indefinitely because mcp-go creates an orphan session and blocks.
server.WithDisableStreaming(true),
server.WithHTTPContextFunc(func(ctx context.Context, r *http.Request) context.Context {
// Extract W3C Trace Context from HTTP headers, or generate a new one
traceCtx, err := trace.FromHTTPHeadersOrNew(r.Header)
if err != nil {
logger.Warn("Failed to generate trace context, continuing without trace", "error", err)
// Continue without trace context if generation fails
} else {
ctx = trace.ContextWithTraceContext(ctx, traceCtx)
}

// Note: HTTP request logging is handled by MCP hooks (OnBeforeAny, OnSuccess, OnError)
// which provide more detailed information including method, params, and results.

return httpContextFunc(ctx, r, cfg.BaseURL)
}),
)
httpServer := newStreamableHTTPServer(mcpServer, logger, func(ctx context.Context, r *http.Request) context.Context {
// Extract W3C Trace Context from HTTP headers, or generate a new one
traceCtx, err := trace.FromHTTPHeadersOrNew(r.Header)
if err != nil {
logger.Warn("Failed to generate trace context, continuing without trace", "error", err)
// Continue without trace context if generation fails
} else {
ctx = trace.ContextWithTraceContext(ctx, traceCtx)
}

// Note: HTTP request logging is handled by MCP hooks (OnBeforeAny, OnSuccess, OnError)
// which provide more detailed information including method, params, and results.

return httpContextFunc(ctx, r, cfg.BaseURL)
})

mux := http.NewServeMux()
mux.Handle("/mcp", httpServer)
Expand Down
41 changes: 41 additions & 0 deletions internal/flashduty/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package flashduty

import (
"context"
"io"
"log/slog"
"net/http"
"reflect"
"testing"

"github.com/flashcatcloud/flashduty-mcp-server/pkg/translations"
)

func TestNewStreamableHTTPServer_DoesNotDisableStreaming(t *testing.T) {
t.Parallel()

mcpServer, err := NewMCPServer(FlashdutyConfig{
Version: "test",
Translator: translations.NullTranslationHelper,
EnabledToolsets: []string{"incidents"},
})
if err != nil {
t.Fatalf("failed to create MCP server: %v", err)
}

httpServer := newStreamableHTTPServer(
mcpServer,
slog.New(slog.NewTextHandler(io.Discard, nil)),
func(ctx context.Context, _ *http.Request) context.Context {
return ctx
},
)

value := reflect.ValueOf(httpServer).Elem().FieldByName("disableStreaming")
if !value.IsValid() {
t.Fatal("expected streamable HTTP server to expose disableStreaming field")
}
if value.Bool() {
t.Fatal("expected streaming to remain enabled for GET/SSE clients")
}
}
Loading
Loading