GeezerVison is showing github.com — fix(mcp): remove writeEnabled from Start() to prevent readonly state … · knative/func@70fd6e8
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: cmd/mcp.go +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -118,6 +118,6 @@ func runMCPStart(cmd *cobra.Command, args []string, newClient ClientFactory) err | 118 | 118 | defer done() | 119 | 119 |
| 120 | 120 | // Start | 121 | | - return client.StartMCPServer(cmd.Context(), writeEnabled) | | 121 | + return client.StartMCPServer(cmd.Context()) | 122 | 122 |
| 123 | 123 | } |
Collapse file Copy file name to clipboardExpand all lines: cmd/mcp_test.go +11-17Lines changed: 11 additions & 17 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | 1 | package cmd | 2 | 2 |
| 3 | 3 | import ( | 4 | | - "context" | 5 | 4 | "testing" | 6 | 5 |
| 7 | 6 | fn "knative.dev/func/pkg/functions" | @@ -30,37 +29,32 @@ func TestMCP_Start(t *testing.T) { | 30 | 29 | } | 31 | 30 | } | 32 | 31 |
| 33 | | -// TestMCP_StartWriteable ensures that the server is started with write | 34 | | -// enabled only when the environment variable FUNC_ENABLE_MCP_WRITE is set. | | 32 | +// TestMCP_StartWriteable ensures that the FUNC_ENABLE_MCP_WRITE environment | | 33 | +// variable is correctly parsed and the server starts in both default | | 34 | +// (readonly) and write-enabled modes. | 35 | 35 | func TestMCP_StartWriteable(t *testing.T) { | 36 | 36 | _ = FromTempDirectory(t) | 37 | 37 |
| 38 | | - // Ensure it defaults to off. | | 38 | + // Ensure it defaults to readonly (no env var set). | 39 | 39 | server := mock.NewMCPServer() | 40 | | - server.StartFn = func(_ context.Context, writeEnabled bool) error { | 41 | | - if writeEnabled { | 42 | | - t.Fatal("MCP server started write-enabled by default") | 43 | | - } | 44 | | - return nil | 45 | | - } | 46 | 40 | cmd := NewMCPCmd(NewTestClient(fn.WithMCPServer(server))) | 47 | 41 | cmd.SetArgs([]string{"start"}) | 48 | 42 | if err := cmd.Execute(); err != nil { | 49 | 43 | t.Fatal(err) | 50 | 44 | } | | 45 | + if !server.StartInvoked { | | 46 | + t.Fatal("MCP server was not started in default mode") | | 47 | + } | 51 | 48 |
| 52 | | - // Ensure it is set to true on proper truthy value | | 49 | + // Ensure it starts successfully with write mode enabled. | 53 | 50 | t.Setenv("FUNC_ENABLE_MCP_WRITE", "true") | 54 | 51 | server = mock.NewMCPServer() | 55 | | - server.StartFn = func(_ context.Context, writeEnabled bool) error { | 56 | | - if !writeEnabled { | 57 | | - t.Fatal("MCP server was not enabled") | 58 | | - } | 59 | | - return nil | 60 | | - } | 61 | 52 | cmd = NewMCPCmd(NewTestClient(fn.WithMCPServer(server))) | 62 | 53 | cmd.SetArgs([]string{"start"}) | 63 | 54 | if err := cmd.Execute(); err != nil { | 64 | 55 | t.Fatal(err) | 65 | 56 | } | | 57 | + if !server.StartInvoked { | | 58 | + t.Fatal("MCP server was not started with write mode enabled") | | 59 | + } | 66 | 60 | } |
Collapse file Copy file name to clipboardExpand all lines: pkg/functions/client.go +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -227,7 +227,7 @@ type PipelinesProvider interface { | 227 | 227 | // MCPServer for a given client instance which performs bidirectional | 228 | 228 | // communication with a client agent. | 229 | 229 | type MCPServer interface { | 230 | | - Start(context.Context, bool) error | | 230 | + Start(context.Context) error | 231 | 231 | } | 232 | 232 |
| 233 | 233 | // New client for function management. | @@ -1266,8 +1266,8 @@ func (c *Client) Push(ctx context.Context, f Function) (Function, bool, error) { | 1266 | 1266 |
| 1267 | 1267 | // StartMCPServer is currently a passthrough to the configured MCP Server | 1268 | 1268 | // instance. | 1269 | | -func (c *Client) StartMCPServer(ctx context.Context, writeEnabled bool) error { | 1270 | | - return c.mcpServer.Start(ctx, writeEnabled) | | 1269 | +func (c *Client) StartMCPServer(ctx context.Context) error { | | 1270 | + return c.mcpServer.Start(ctx) | 1271 | 1271 | } | 1272 | 1272 |
| 1273 | 1273 | // ensureRunDataDir creates a .func directory at the given path, and | @@ -1555,4 +1555,4 @@ func (n *noopDNSProvider) Provide(_ Function) error { return nil } | 1555 | 1555 | // MCPServer | 1556 | 1556 | type noopMCPServer struct{} | 1557 | 1557 |
| 1558 | | -func (n *noopMCPServer) Start(_ context.Context, _ bool) error { return nil } | | 1558 | +func (n *noopMCPServer) Start(_ context.Context) error { return nil } |
Collapse file Copy file name to clipboardExpand all lines: pkg/functions/client_test.go +10-1Lines changed: 10 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -1286,7 +1286,7 @@ func TestClient_StartMCPServer(t *testing.T) { | 1286 | 1286 |
| 1287 | 1287 | client := fn.New(fn.WithMCPServer(server)) | 1288 | 1288 |
| 1289 | | - if err := client.StartMCPServer(t.Context(), true); err != nil { | | 1289 | + if err := client.StartMCPServer(t.Context()); err != nil { | 1290 | 1290 | t.Fatal(err) | 1291 | 1291 | } | 1292 | 1292 |
| @@ -1295,6 +1295,15 @@ func TestClient_StartMCPServer(t *testing.T) { | 1295 | 1295 | } | 1296 | 1296 | } | 1297 | 1297 |
| | 1298 | +// TestClient_StartMCPServer_Default ensures the default noop MCP server | | 1299 | +// does not error when no server is explicitly configured. | | 1300 | +func TestClient_StartMCPServer_Default(t *testing.T) { | | 1301 | + client := fn.New() // no WithMCPServer — uses noopMCPServer | | 1302 | + if err := client.StartMCPServer(t.Context()); err != nil { | | 1303 | + t.Fatal(err) | | 1304 | + } | | 1305 | +} | | 1306 | + | 1298 | 1307 | // TestClient_List_OutsideRoot ensures that a call to a function (in this case list) | 1299 | 1308 | // that is not contextually dependent on being associated with a function, | 1300 | 1309 | // can be run from anywhere, thus ensuring that the client itself makes | |
Collapse file Copy file name to clipboardExpand all lines: pkg/mcp/mcp.go +4-3Lines changed: 4 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -157,9 +157,10 @@ func New(options ...Option) *Server { | 157 | 157 | return s | 158 | 158 | } | 159 | 159 |
| 160 | | -// Start the MCP server using the configured transport | 161 | | -func (s *Server) Start(ctx context.Context, writeEnabled bool) error { | 162 | | - s.readonly.Store(!writeEnabled) | | 160 | +// Start the MCP server using the configured transport. | | 161 | +// The server's readonly mode is determined at construction time via | | 162 | +// WithReadonly; it cannot be changed after the server is created. | | 163 | +func (s *Server) Start(ctx context.Context) error { | 163 | 164 | return s.impl.Run(ctx, s.transport) | 164 | 165 | } | 165 | 166 |
| |
Collapse file Copy file name to clipboardExpand all lines: pkg/mcp/mcp_test.go +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -84,7 +84,7 @@ func newTestPairCore(t *testing.T, readonly bool, options ...Option) (session *m | 84 | 84 |
| 85 | 85 | // Start the Server | 86 | 86 | go func() { | 87 | | - errCh <- server.Start(t.Context(), !readonly) | | 87 | + errCh <- server.Start(t.Context()) | 88 | 88 | }() | 89 | 89 |
| 90 | 90 | // Connect a client to trigger initialization | |
Collapse file Copy file name to clipboardExpand all lines: pkg/mock/mcp_server.go +4-4Lines changed: 4 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -6,16 +6,16 @@ import ( | 6 | 6 |
| 7 | 7 | type MCPServer struct { | 8 | 8 | StartInvoked bool | 9 | | - StartFn func(context.Context, bool) error | | 9 | + StartFn func(context.Context) error | 10 | 10 | } | 11 | 11 |
| 12 | 12 | func NewMCPServer() *MCPServer { | 13 | 13 | return &MCPServer{ | 14 | | - StartFn: func(context.Context, bool) error { return nil }, | | 14 | + StartFn: func(context.Context) error { return nil }, | 15 | 15 | } | 16 | 16 | } | 17 | 17 |
| 18 | | -func (s *MCPServer) Start(ctx context.Context, writeEnabled bool) error { | | 18 | +func (s *MCPServer) Start(ctx context.Context) error { | 19 | 19 | s.StartInvoked = true | 20 | | - return s.StartFn(ctx, writeEnabled) | | 20 | + return s.StartFn(ctx) | 21 | 21 | } |
Collapse file Copy file name to clipboard +21Lines changed: 21 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +package mock | | 2 | + | | 3 | +import ( | | 4 | + "context" | | 5 | + "testing" | | 6 | +) | | 7 | + | | 8 | +func TestMCPServer_Start(t *testing.T) { | | 9 | + s := NewMCPServer() | | 10 | + if s.StartInvoked { | | 11 | + t.Fatal("StartInvoked should be false before calling Start") | | 12 | + } | | 13 | + | | 14 | + err := s.Start(context.Background()) | | 15 | + if err != nil { | | 16 | + t.Fatalf("unexpected error: %v", err) | | 17 | + } | | 18 | + if !s.StartInvoked { | | 19 | + t.Fatal("StartInvoked should be true after calling Start") | | 20 | + } | | 21 | +} |
|