Skip to content

feature:tool result add command type #468

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
9 changes: 9 additions & 0 deletions mcp/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,15 @@ type EmbeddedResource struct {

func (EmbeddedResource) isContent() {}

// CommandContent represents a command to be executed by the client.
type CommandContent struct {
Type string `json:"type"` // must be "command"
Command string `json:"command"` // the command name
Params map[string]interface{} `json:"params,omitempty"` // optional parameters
}

func (CommandContent) isContent() {}

// ModelPreferences represents the server's preferences for model selection,
// requested of the client during sampling.
//
Expand Down
30 changes: 30 additions & 0 deletions mcp/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ func AsBlobResourceContents(content any) (*BlobResourceContents, bool) {
return asType[BlobResourceContents](content)
}

// AsCommandContent attempts to cast the given interface to CommandContent
func AsCommandContent(content any) (*CommandContent, bool) {
return asType[CommandContent](content)
}

// Helper function for JSON-RPC

// NewJSONRPCResponse creates a new JSONRPCResponse with the given id and result
Expand Down Expand Up @@ -353,6 +358,19 @@ func NewToolResultErrorf(format string, a ...any) *CallToolResult {
}
}

// NewToolResultCommand creates a CallToolResult with a command content.
func NewToolResultCommand(command string, params map[string]any) *CallToolResult {
return &CallToolResult{
Content: []Content{
CommandContent{
Type: "command",
Command: command,
Params: params,
},
},
}
}

// NewListResourcesResult creates a new ListResourcesResult
func NewListResourcesResult(
resources []Resource,
Expand Down Expand Up @@ -509,6 +527,18 @@ func ParseContent(contentMap map[string]any) (Content, error) {
}

return NewEmbeddedResource(resourceContents), nil

case "command":
command := ExtractString(contentMap, "command")
if command == "" {
return nil, fmt.Errorf("command name is missing")
}
params := ExtractMap(contentMap, "params")
return CommandContent{
Type: "command",
Command: command,
Params: params,
}, nil
}

return nil, fmt.Errorf("unsupported content type: %s", contentType)
Expand Down