GeezerVison is showing github.com — fix(mcp): use atomic.Bool for Server.readonly to prevent data races · knative/func@09b5041
File treeExpand file treeCollapse file tree Open diff view settings third_party/VENDOR-LICENSE/github.com AlecAivazis/survey/v2/terminal
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: pkg/mcp/mcp.go +5-4Lines changed: 5 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 4 | 4 | "context" | 5 | 5 | "os/exec" | 6 | 6 | "strings" | | 7 | + "sync/atomic" | 7 | 8 |
| 8 | 9 | "github.com/modelcontextprotocol/go-sdk/mcp" | 9 | 10 | ) | @@ -22,7 +23,7 @@ const ( | 22 | 23 | type Server struct { | 23 | 24 | OnInit func(context.Context) // Invoked when the server is initialized | 24 | 25 | prefix string // Command prefix ("func" or "kn func") | 25 | | - readonly bool // disables deploy and delete when true | | 26 | + readonly atomic.Bool // disables deploy and delete when true | 26 | 27 | executor executor | 27 | 28 | transport mcp.Transport // Transport to use (defaults to StdioTransport) | 28 | 29 | impl *mcp.Server // implements the protocol | @@ -58,7 +59,7 @@ func WithTransport(transport mcp.Transport) Option { | 58 | 59 | // WithReadonly sets the server to readonly mode. | 59 | 60 | func WithReadonly(readonly bool) Option { | 60 | 61 | return func(s *Server) { | 61 | | - s.readonly = readonly | | 62 | + s.readonly.Store(readonly) | 62 | 63 | } | 63 | 64 | } | 64 | 65 |
| @@ -80,7 +81,7 @@ func New(options ...Option) *Server { | 80 | 81 | Title: title, | 81 | 82 | Version: version}, | 82 | 83 | &mcp.ServerOptions{ | 83 | | - Instructions: instructions(s.readonly), | | 84 | + Instructions: instructions(s.readonly.Load()), | 84 | 85 | HasPrompts: true, | 85 | 86 | HasResources: true, | 86 | 87 | HasTools: true, | @@ -146,7 +147,7 @@ func New(options ...Option) *Server { | 146 | 147 |
| 147 | 148 | // Start the MCP server using the configured transport | 148 | 149 | func (s *Server) Start(ctx context.Context, writeEnabled bool) error { | 149 | | - s.readonly = !writeEnabled | | 150 | + s.readonly.Store(!writeEnabled) | 150 | 151 | return s.impl.Run(ctx, s.transport) | 151 | 152 | } | 152 | 153 |
| |
Collapse file Copy file name to clipboardExpand all lines: pkg/mcp/tools_delete.go +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -20,7 +20,7 @@ var deleteTool = &mcp.Tool{ | 20 | 20 | } | 21 | 21 |
| 22 | 22 | func (s *Server) deleteHandler(ctx context.Context, r *mcp.CallToolRequest, input DeleteInput) (result *mcp.CallToolResult, output DeleteOutput, err error) { | 23 | | - if s.readonly { | | 23 | + if s.readonly.Load() { | 24 | 24 | err = fmt.Errorf("the server is currently in readonly mode. Please set FUNC_ENABLE_MCP_WRITE and restart the client") | 25 | 25 | return | 26 | 26 | } | |
Collapse file Copy file name to clipboardExpand all lines: pkg/mcp/tools_delete_test.go +20-1Lines changed: 20 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -55,7 +55,7 @@ func TestTool_Delete_Args(t *testing.T) { | 55 | 55 | t.Fatal(err) | 56 | 56 | } | 57 | 57 | // Delete requires write mode - enable it for this test | 58 | | - server.readonly = false | | 58 | + server.readonly.Store(false) | 59 | 59 |
| 60 | 60 | // Build input arguments from test data | 61 | 61 | inputArgs := buildInputArgs(stringFlags, boolFlags) | @@ -76,3 +76,22 @@ func TestTool_Delete_Args(t *testing.T) { | 76 | 76 | t.Fatal("executor was not invoked") | 77 | 77 | } | 78 | 78 | } | | 79 | + | | 80 | +// TestTool_Delete_Readonly ensures the delete tool rejects requests in readonly mode. | | 81 | +func TestTool_Delete_Readonly(t *testing.T) { | | 82 | + client, _, err := newTestPairWithReadonly(t, true) // readonly = true | | 83 | + if err != nil { | | 84 | + t.Fatal(err) | | 85 | + } | | 86 | + | | 87 | + result, err := client.CallTool(t.Context(), &mcp.CallToolParams{ | | 88 | + Name: "delete", | | 89 | + Arguments: map[string]any{"name": "my-function"}, | | 90 | + }) | | 91 | + if err != nil { | | 92 | + t.Fatal(err) | | 93 | + } | | 94 | + if !result.IsError { | | 95 | + t.Fatal("expected delete to be rejected in readonly mode") | | 96 | + } | | 97 | +} |
Collapse file Copy file name to clipboardExpand all lines: pkg/mcp/tools_deploy.go +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -20,7 +20,7 @@ var deployTool = &mcp.Tool{ | 20 | 20 | } | 21 | 21 |
| 22 | 22 | func (s *Server) deployHandler(ctx context.Context, r *mcp.CallToolRequest, input DeployInput) (result *mcp.CallToolResult, output DeployOutput, err error) { | 23 | | - if s.readonly { | | 23 | + if s.readonly.Load() { | 24 | 24 | err = fmt.Errorf("the server is currently in readonly mode. Please set FUNC_ENABLE_MCP_WRITE and restart the client") | 25 | 25 | return | 26 | 26 | } | |
Collapse file Copy file name to clipboardExpand all lines: pkg/mcp/tools_deploy_test.go +20-1Lines changed: 20 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -59,7 +59,7 @@ func TestTool_Deploy_Args(t *testing.T) { | 59 | 59 | t.Fatal(err) | 60 | 60 | } | 61 | 61 | // Deploy requires write mode - enable it for this test | 62 | | - server.readonly = false | | 62 | + server.readonly.Store(false) | 63 | 63 |
| 64 | 64 | // Build input arguments from test data | 65 | 65 | inputArgs := buildInputArgs(stringFlags, boolFlags) | @@ -79,3 +79,22 @@ func TestTool_Deploy_Args(t *testing.T) { | 79 | 79 | t.Fatal("executor was not invoked") | 80 | 80 | } | 81 | 81 | } | | 82 | + | | 83 | +// TestTool_Deploy_Readonly ensures the deploy tool rejects requests in readonly mode. | | 84 | +func TestTool_Deploy_Readonly(t *testing.T) { | | 85 | + client, _, err := newTestPairWithReadonly(t, true) // readonly = true | | 86 | + if err != nil { | | 87 | + t.Fatal(err) | | 88 | + } | | 89 | + | | 90 | + result, err := client.CallTool(t.Context(), &mcp.CallToolParams{ | | 91 | + Name: "deploy", | | 92 | + Arguments: map[string]any{"path": "."}, | | 93 | + }) | | 94 | + if err != nil { | | 95 | + t.Fatal(err) | | 96 | + } | | 97 | + if !result.IsError { | | 98 | + t.Fatal("expected deploy to be rejected in readonly mode") | | 99 | + } | | 100 | +} |
Collapse file Copy file name to clipboard +22-22Lines changed: 22 additions & 22 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -Copyright (c) 2014 Takashi Kokubun | 2 | | -
| 3 | | -MIT License | 4 | | -
| 5 | | -Permission is hereby granted, free of charge, to any person obtaining | 6 | | -a copy of this software and associated documentation files (the | 7 | | -"Software"), to deal in the Software without restriction, including | 8 | | -without limitation the rights to use, copy, modify, merge, publish, | 9 | | -distribute, sublicense, and/or sell copies of the Software, and to | 10 | | -permit persons to whom the Software is furnished to do so, subject to | 11 | | -the following conditions: | 12 | | -
| 13 | | -The above copyright notice and this permission notice shall be | 14 | | -included in all copies or substantial portions of the Software. | 15 | | -
| 16 | | -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | 17 | | -EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | 18 | | -MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | 19 | | -NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | 20 | | -LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | 21 | | -OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | 22 | | -WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | | 1 | +Copyright (c) 2014 Takashi Kokubun | | 2 | + | | 3 | +MIT License | | 4 | + | | 5 | +Permission is hereby granted, free of charge, to any person obtaining | | 6 | +a copy of this software and associated documentation files (the | | 7 | +"Software"), to deal in the Software without restriction, including | | 8 | +without limitation the rights to use, copy, modify, merge, publish, | | 9 | +distribute, sublicense, and/or sell copies of the Software, and to | | 10 | +permit persons to whom the Software is furnished to do so, subject to | | 11 | +the following conditions: | | 12 | + | | 13 | +The above copyright notice and this permission notice shall be | | 14 | +included in all copies or substantial portions of the Software. | | 15 | + | | 16 | +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | | 17 | +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | | 18 | +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | | 19 | +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | | 20 | +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | | 21 | +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | | 22 | +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Collapse file Copy file name to clipboard +9-9Lines changed: 9 additions & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -y.output | 2 | | - | 3 | | -# ignore intellij files | 4 | | -.idea | 5 | | -*.iml | 6 | | -*.ipr | 7 | | -*.iws | 8 | | - | 9 | | -*.test | | 1 | +y.output | | 2 | + | | 3 | +# ignore intellij files | | 4 | +.idea | | 5 | +*.iml | | 6 | +*.ipr | | 7 | +*.iws | | 8 | + | | 9 | +*.test |
Collapse file Copy file name to clipboard +18-18Lines changed: 18 additions & 18 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -TEST?=./... | 2 | | - | 3 | | -default: test | 4 | | - | 5 | | -fmt: generate | 6 | | - go fmt ./... | 7 | | - | 8 | | -test: generate | 9 | | - go get -t ./... | 10 | | - go test $(TEST) $(TESTARGS) | 11 | | - | 12 | | -generate: | 13 | | - go generate ./... | 14 | | - | 15 | | -updatedeps: | 16 | | - go get -u golang.org/x/tools/cmd/stringer | 17 | | - | 18 | | -.PHONY: default generate test updatedeps | | 1 | +TEST?=./... | | 2 | + | | 3 | +default: test | | 4 | + | | 5 | +fmt: generate | | 6 | + go fmt ./... | | 7 | + | | 8 | +test: generate | | 9 | + go get -t ./... | | 10 | + go test $(TEST) $(TESTARGS) | | 11 | + | | 12 | +generate: | | 13 | + go generate ./... | | 14 | + | | 15 | +updatedeps: | | 16 | + go get -u golang.org/x/tools/cmd/stringer | | 17 | + | | 18 | +.PHONY: default generate test updatedeps |
Collapse file Copy file name to clipboard +5-5Lines changed: 5 additions & 5 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -resource = [{ | 2 | | - foo = [{ | 3 | | - bar = {} | 4 | | - }] | 5 | | -}] | | 1 | +resource = [{ | | 2 | + foo = [{ | | 3 | + bar = {} | | 4 | + }] | | 5 | +}] |
Collapse file Copy file name to clipboard +15-15Lines changed: 15 additions & 15 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 1 | | -// Foo | 2 | | - | 3 | | -/* Bar */ | 4 | | - | 5 | | -/* | 6 | | -/* | 7 | | -Baz | 8 | | -*/ | 9 | | - | 10 | | -# Another | 11 | | - | 12 | | -# Multiple | 13 | | -# Lines | 14 | | - | 15 | | -foo = "bar" | | 1 | +// Foo | | 2 | + | | 3 | +/* Bar */ | | 4 | + | | 5 | +/* | | 6 | +/* | | 7 | +Baz | | 8 | +*/ | | 9 | + | | 10 | +# Another | | 11 | + | | 12 | +# Multiple | | 13 | +# Lines | | 14 | + | | 15 | +foo = "bar" |
|