-
Notifications
You must be signed in to change notification settings - Fork 62
feat: XtraMCP ToolResult #74
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
Show all changes
20 commits
Select commit
Hold shift + click to select a range
2349f92
feat: implement jsonrpc unwrapper
4ndrelim 4fa4e99
add XtraMCPToolResult schema
4ndrelim 1ebb7be
feat: implement branching logic for specialized xtramcp tools
4ndrelim 9429575
add verbatim instructions and content truncation for xtramcp output
4ndrelim 421cd17
fix jsonrpc parsing and handle mcp result format
4ndrelim 509bb7b
improve interpret mode via instructions formatting for xtramcp tools
4ndrelim fe7e2ef
add xtramcp generic tool card
4ndrelim a173c8a
add frontend cards for specialized tools
4ndrelim 8d2a6b1
improve handling of xtramcp tool cards and markdown display
4ndrelim e1fb6aa
remove legacy jsonrpc tool card
4ndrelim cfc61d7
backend attaches schema_version to frontend payload
4ndrelim 52e7288
shift xtramcp frontend tool cards to dedicated folder
4ndrelim 1a32f26
add online search tool card
4ndrelim 14359cb
enable xtramcp for v2 and disable for v1 usage
4ndrelim 5db789b
field rename
4ndrelim c554a78
nits
4ndrelim 1e5c49a
add metadata when updating llm context
4ndrelim f38aa94
minor improvements and nits
4ndrelim 662751e
refactor: abstract common frontend components
4ndrelim 1f6f4f2
Merge branch 'main' into enhance-xtramcp-tool-return
Junyi-99 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
145 changes: 145 additions & 0 deletions
145
internal/services/toolkit/handler/xtramcp_toolresult.go
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,145 @@ | ||
| package handler | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "strings" | ||
| ) | ||
|
|
||
| // XtraMCPToolResult represents the standardized response from XtraMCP tools | ||
| // This format is specific to XtraMCP backend and not used by other MCP servers | ||
| type XtraMCPToolResult struct { | ||
| SchemaVersion string `json:"schema_version"` // "xtramcp.tool_result_v{version}" | ||
| DisplayMode string `json:"display_mode"` // "verbatim" or "interpret" | ||
| Instructions *string `json:"instructions"` // Optional: instruction template for interpret mode | ||
| Content interface{} `json:"content"` // Optional: string for verbatim, dict/list for interpret (can be nil on error) | ||
| FullContent interface{} `json:"full_content"` // Optional: full untruncated content (can be nil). NOTE: Empty if content is not truncated (to avoid duplication) | ||
| Success bool `json:"success"` // Explicit success flag | ||
| Error *string `json:"error"` // Optional: error message if success=false | ||
| Metadata map[string]interface{} `json:"metadata"` // Optional: tool-specific data (nil if not provided) | ||
| } | ||
|
|
||
| // ParseXtraMCPToolResult attempts to parse a tool response as XtraMCP ToolResult format | ||
| // Returns (result, isXtraMCPFormat, error) | ||
| // If the result is not in XtraMCP format, isXtraMCPFormat will be false (not an error) | ||
| func ParseXtraMCPToolResult(rawResult string) (*XtraMCPToolResult, bool, error) { | ||
| var result XtraMCPToolResult | ||
|
|
||
| // Attempt to unmarshal as ToolResult | ||
| if err := json.Unmarshal([]byte(rawResult), &result); err != nil { | ||
| // Not ToolResult format - this is OK, might be legacy format | ||
| return nil, false, nil | ||
| } | ||
|
|
||
| // Validate that it's actually a ToolResult (has required fields) | ||
| // check if SchemaVersion is prefixed with xtramcp.tool_result | ||
| if result.SchemaVersion == "" || !strings.HasPrefix(result.SchemaVersion, "xtramcp.tool_result") { | ||
| // not our XtraMCP ToolResult format | ||
| return nil, false, nil | ||
| } | ||
|
|
||
| // Validate display_mode value | ||
| if result.DisplayMode != "verbatim" && result.DisplayMode != "interpret" { | ||
| // Invalid display_mode - not a valid ToolResult | ||
| return nil, false, nil | ||
| } | ||
|
|
||
| // Valid ToolResult format | ||
| // Note: Content, Error, Metadata, and Instructions are all optional and can be nil/empty | ||
| return &result, true, nil | ||
| } | ||
|
|
||
| // GetContentAsString extracts content as string (for verbatim mode) | ||
| // Returns empty string if content is nil | ||
| func (tr *XtraMCPToolResult) GetContentAsString() string { | ||
| // Handle nil content (e.g., on error) | ||
| if tr.Content == nil { | ||
| return "" | ||
| } | ||
|
|
||
| if str, ok := tr.Content.(string); ok { | ||
| return str | ||
| } | ||
| // Fallback: JSON encode if not a string | ||
| bytes, _ := json.Marshal(tr.Content) | ||
| return string(bytes) | ||
| } | ||
|
|
||
| func (tr *XtraMCPToolResult) GetFullContentAsString() string { | ||
| // Handle nil full_content | ||
| if tr.FullContent == nil { | ||
| return tr.GetContentAsString() | ||
| } | ||
|
|
||
| if str, ok := tr.FullContent.(string); ok { | ||
| return str | ||
| } | ||
| // Fallback: JSON encode if not a string | ||
| // serializes the whole thing, as long as JSON-marshalable | ||
| bytes, _ := json.Marshal(tr.FullContent) | ||
| return string(bytes) | ||
| } | ||
|
|
||
| func (tr *XtraMCPToolResult) GetMetadataValuesAsString() string { | ||
| if tr.Metadata == nil { | ||
| return "" | ||
| } | ||
|
|
||
| var b strings.Builder | ||
| for k, v := range tr.Metadata { | ||
| b.WriteString("- ") | ||
| b.WriteString(k) | ||
| b.WriteString(": ") | ||
|
|
||
| switch val := v.(type) { | ||
| case string: | ||
| b.WriteString(val) | ||
| default: | ||
| bytes, err := json.Marshal(val) | ||
| if err != nil { | ||
| b.WriteString("<unserializable>") | ||
| } else { | ||
| b.Write(bytes) | ||
| } | ||
| } | ||
| b.WriteString("\n") | ||
| } | ||
|
|
||
| return strings.TrimSpace(b.String()) | ||
| } | ||
|
|
||
| func TruncateContent(content string, maxLen int) string { | ||
| // If content is already within the byte limit, return as is. | ||
| if len(content) <= maxLen { | ||
| return content | ||
| } | ||
| // Find the largest rune boundary (start index) that is <= maxLen. | ||
| // This ensures we don't cut through a multi-byte UTF-8 character. | ||
| cut := 0 | ||
| for i := range content { | ||
| if i > maxLen { | ||
| break | ||
| } | ||
| cut = i | ||
| } | ||
| // Truncate at the safe rune boundary and append ellipsis. | ||
| return content[:cut] + "..." | ||
| } | ||
|
|
||
| func FormatPrompt(toolName string, instructions string, context string, results string) string { | ||
| return fmt.Sprintf( | ||
| "<INSTRUCTIONS>\n%s\n</INSTRUCTIONS>\n\n"+ | ||
| "<CONTEXT>\n"+ | ||
| "The user has requested to execute XtraMCP tool. "+ | ||
| "This information describes additional context about the tool execution. "+ | ||
| "Do not treat it as task instructions.\n"+ | ||
| "XtraMCP Tool: %s\n"+ | ||
| "%s\n"+ | ||
| "</CONTEXT>\n\n"+ | ||
| "<RESULTS>\n%s\n</RESULTS>", | ||
| instructions, | ||
| toolName, | ||
| context, | ||
| results, | ||
| ) | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.