diff --git a/mcp/types.go b/mcp/types.go index d4f6132c8..9ddaf420b 100644 --- a/mcp/types.go +++ b/mcp/types.go @@ -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. // diff --git a/mcp/utils.go b/mcp/utils.go index 3e652efd7..a7ebc1af2 100644 --- a/mcp/utils.go +++ b/mcp/utils.go @@ -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 @@ -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, @@ -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)