-
Notifications
You must be signed in to change notification settings - Fork 312
feat(mcp): refactor to use go-sdk #385
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,109 @@ | ||
| package mcp | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
|
|
||
| "github.com/containers/kubernetes-mcp-server/pkg/api" | ||
| "github.com/modelcontextprotocol/go-sdk/mcp" | ||
| "k8s.io/utils/ptr" | ||
| ) | ||
|
|
||
| func ServerToolToGoSdkTool(s *Server, tool api.ServerTool) (*mcp.Tool, mcp.ToolHandler, error) { | ||
| goSdkTool := &mcp.Tool{ | ||
| Name: tool.Tool.Name, | ||
| Description: tool.Tool.Description, | ||
| Title: tool.Tool.Annotations.Title, | ||
| Annotations: &mcp.ToolAnnotations{ | ||
| Title: tool.Tool.Annotations.Title, | ||
| ReadOnlyHint: ptr.Deref(tool.Tool.Annotations.ReadOnlyHint, false), | ||
| DestructiveHint: tool.Tool.Annotations.DestructiveHint, | ||
| IdempotentHint: ptr.Deref(tool.Tool.Annotations.IdempotentHint, false), | ||
| OpenWorldHint: tool.Tool.Annotations.OpenWorldHint, | ||
| }, | ||
| } | ||
| if tool.Tool.InputSchema != nil { | ||
| schema, err := json.Marshal(tool.Tool.InputSchema) | ||
| if err != nil { | ||
| return nil, nil, fmt.Errorf("failed to marshal tool input schema for tool %s: %v", tool.Tool.Name, err) | ||
| } | ||
| // TODO: temporary fix to append an empty properties object (some client have trouble parsing a schema without properties) | ||
| // As opposed, Gemini had trouble for a while when properties was present but empty. | ||
| // https://github.com/containers/kubernetes-mcp-server/issues/340 | ||
| if string(schema) == `{"type":"object"}` { | ||
| schema = []byte(`{"type":"object","properties":{}}`) | ||
| } | ||
|
|
||
| var fixedSchema map[string]interface{} | ||
| if err := json.Unmarshal(schema, &fixedSchema); err != nil { | ||
| return nil, nil, fmt.Errorf("failed to unmarshal tool input schema for tool %s: %v", tool.Tool.Name, err) | ||
| } | ||
|
|
||
| goSdkTool.InputSchema = fixedSchema | ||
| } | ||
| goSdkHandler := func(ctx context.Context, request *mcp.CallToolRequest) (*mcp.CallToolResult, error) { | ||
| toolCallRequest, err := GoSdkToolCallRequestToToolCallRequest(request) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("%v for tool %s", err, tool.Tool.Name) | ||
| } | ||
| // get the correct derived Kubernetes client for the target specified in the request | ||
| cluster := toolCallRequest.GetString(s.p.GetTargetParameterName(), s.p.GetDefaultTarget()) | ||
| k, err := s.p.GetDerivedKubernetes(ctx, cluster) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| result, err := tool.Handler(api.ToolHandlerParams{ | ||
| Context: ctx, | ||
| Kubernetes: k, | ||
| ToolCallRequest: toolCallRequest, | ||
| ListOutput: s.configuration.ListOutput(), | ||
| }) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return NewTextResult(result.Content, result.Error), nil | ||
| } | ||
| return goSdkTool, goSdkHandler, nil | ||
| } | ||
|
|
||
| type ToolCallRequest struct { | ||
| Name string | ||
| arguments map[string]any | ||
| } | ||
|
|
||
| var _ api.ToolCallRequest = (*ToolCallRequest)(nil) | ||
|
|
||
| func GoSdkToolCallRequestToToolCallRequest(request *mcp.CallToolRequest) (*ToolCallRequest, error) { | ||
| toolCallParams, ok := request.GetParams().(*mcp.CallToolParamsRaw) | ||
| if !ok { | ||
| return nil, errors.New("invalid tool call parameters for tool call request") | ||
| } | ||
| return GoSdkToolCallParamsToToolCallRequest(toolCallParams) | ||
| } | ||
|
|
||
| func GoSdkToolCallParamsToToolCallRequest(toolCallParams *mcp.CallToolParamsRaw) (*ToolCallRequest, error) { | ||
| var arguments map[string]any | ||
| if err := json.Unmarshal(toolCallParams.Arguments, &arguments); err != nil { | ||
| return nil, fmt.Errorf("failed to unmarshal tool call arguments: %v", err) | ||
| } | ||
| return &ToolCallRequest{ | ||
| Name: toolCallParams.Name, | ||
| arguments: arguments, | ||
| }, nil | ||
| } | ||
|
|
||
| func (ToolCallRequest *ToolCallRequest) GetArguments() map[string]any { | ||
| return ToolCallRequest.arguments | ||
| } | ||
|
|
||
| func (ToolCallRequest *ToolCallRequest) GetString(key, defaultValue string) string { | ||
| if value, ok := ToolCallRequest.arguments[key]; ok { | ||
| if strValue, ok := value.(string); ok { | ||
| return strValue | ||
| } | ||
| } | ||
| return defaultValue | ||
| } |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
some of the tests still have reference to this. Will that clean-up follow in a separate PR?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The tests are still running using the Mark3Labs MCP client.
We can eventually move to just use the official go-sdk mcp client, but I find it more reliable this way though.