Skip to content

Commit 63424a1

Browse files
committed
Support toolcalling without specifying provider
1 parent b47ab90 commit 63424a1

6 files changed

Lines changed: 60 additions & 18 deletions

File tree

prompts/tool.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Before executing any tools:
2424
> 🔁 **Avoid loops:** If you find yourself proposing the same step again, stop and re-evaluate.
2525
> **No validation:** Once a tool is executed, do not re-check it unless new input justifies it
2626
27+
**IMPORTANT** Create a new plan if we find out new information that is relevant to solve the user request.
28+
2729
## Execution
2830

2931
When executing the plan:

src/mcps/gemcode/gemcode.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -719,6 +719,8 @@ func (s *GemCodeService) handleRunShellCommand(args map[string]any) (any, error)
719719
cmd.Stdout = &stdout
720720
cmd.Stderr = &stderr
721721

722+
fmt.Fprintln(os.Stderr, "This is the command: " + command)
723+
722724
err := cmd.Run()
723725

724726
returnCode := 0

src/repl/tools.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,29 +79,34 @@ func callTool(tool *Tool) (string, error) {
7979
}
8080
}
8181

82+
var out bytes.Buffer
83+
var stderr bytes.Buffer
8284
// Combine the tool name and arguments for the mai-tool command
8385
// tool.Name may be in the format "server/tool"
84-
cmdArgs := append([]string{"call", toolName}, safeArgs...)
86+
/*
8587
cmd := exec.Command("mai-tool", cmdArgs...)
8688
87-
var out bytes.Buffer
88-
var stderr bytes.Buffer
8989
cmd.Stdout = &out
9090
cmd.Stderr = &stderr
91+
*/
92+
timeout := 60
93+
cmdArgs := append([]string{"call", toolName}, safeArgs...)
9194

9295
// Set a timeout for the command execution
93-
timeoutCtx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
96+
timeoutCtx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
9497
defer cancel()
9598

9699
// Set the command context with timeout
97-
cmd = exec.CommandContext(timeoutCtx, "mai-tool", cmdArgs...)
100+
cmd := exec.CommandContext(timeoutCtx, "mai-tool", cmdArgs...)
98101
cmd.Stdout = &out
99102
cmd.Stderr = &stderr
103+
fmt.Println("MAITOOLRUN " + toolName)
104+
fmt.Println("MAITOOLARG " + strings.Join(cmdArgs, " '"))
100105

101106
err := cmd.Run()
102107
if err != nil {
103108
if errors.Is(err, context.DeadlineExceeded) {
104-
return "", fmt.Errorf("tool execution timed out after 30 seconds: %s", tool.Name)
109+
return "", fmt.Errorf("tool execution timed out after %d seconds: %s", timeout, tool.Name)
105110
}
106111
return "", fmt.Errorf("error executing tool %s: %v: %s", tool.Name, err, stderr.String())
107112
}
@@ -168,6 +173,9 @@ func mapToArray(m map[string]interface{}) []string {
168173
// extractJSONBlock locates the first balanced JSON object in text (or fenced JSON)
169174
// and returns it plus any remaining tail text.
170175
func extractJSONBlock(text string) (string, string) {
176+
if !strings.Contains(text, "\"plan\"") {
177+
return "", text
178+
}
171179
// Attempt fenced JSON block: ```json ... ```
172180
re := regexp.MustCompile("(?s)```json\\s*(.*?)\\s*```")
173181
matches := re.FindStringSubmatch(text)

src/tool/main.go

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,14 +146,19 @@ func main() {
146146
toolName = slicedText[1]
147147
params = parseParams(flag.Args()[2:])
148148
} else {
149-
if nargs < 3 {
150-
fmt.Println("Error: 'call' requires server and tool name")
151-
fmt.Println("Usage: mcpcli call <server> <tool> [param1=value1] [param2=value2] ...")
152-
os.Exit(1)
153-
}
154-
serverName = flag.Args()[1]
155-
toolName = flag.Args()[2]
156-
params = parseParams(flag.Args()[3:])
149+
serverName = ""
150+
toolName = arg1
151+
params = parseParams(flag.Args()[1:])
152+
/*
153+
if nargs < 3 {
154+
fmt.Println("Error: 'call' requires server and tool name")
155+
fmt.Println("Usage: mcpcli call <server> <tool> [param1=value1] [param2=value2] ...")
156+
os.Exit(1)
157+
}
158+
serverName = flag.Args()[1]
159+
toolName = flag.Args()[2]
160+
params = parseParams(flag.Args()[3:])
161+
*/
157162
}
158163
callTool(config, serverName, toolName, params)
159164
case "servers":
@@ -440,6 +445,8 @@ func listTools(config Config) {
440445
func callTool(config Config, serverName, toolName string, params map[string]string) {
441446
var resp *http.Response
442447
var requestErr error
448+
// fmt.Println("server: "+serverName)
449+
// fmt.Println("tool: "+toolName)
443450

444451
// If serverName contains a slash, it's in the format "server/tool"
445452
if strings.Contains(serverName, "/") {
@@ -453,8 +460,12 @@ func callTool(config Config, serverName, toolName string, params map[string]stri
453460

454461
// Standard tool call for other tools
455462
var endpoint string
456-
// Always use /call endpoint for tool calls
457-
endpoint = fmt.Sprintf("/call/%s/%s", serverName, toolName)
463+
if serverName == "" {
464+
endpoint = fmt.Sprintf("/call/%s", toolName)
465+
} else {
466+
// Always use /call endpoint for tool calls
467+
endpoint = fmt.Sprintf("/call/%s/%s", serverName, toolName)
468+
}
458469

459470
// Build the tool URL
460471
toolUrl := buildApiUrl(config, endpoint)

src/wmcp/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ Shows the status of all running MCP servers.
2727
### Call a Tool
2828
```bash
2929
GET /call/{server}/{tool}?param=value
30+
GET /call/{tool}?param=value
3031
POST /call/{server}/{tool}
32+
POST /call/{tool}
3133
```
32-
Calls a specific tool on a specific server.
34+
Calls a specific tool on a specific server, or uses auto-discovery when only the tool is specified.
3335

3436
## Examples
3537

@@ -156,4 +158,3 @@ curl -X POST "http://localhost:8080/tools/server1/openFile" \
156158
-d '{"invalid": "parameter"}'
157159
# HTTP 400: Tool call failed: missing required parameter 'filePath'
158160
```
159-

src/wmcp/main.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -879,6 +879,21 @@ func (s *MCPService) callToolHandler(w http.ResponseWriter, r *http.Request) {
879879
s.mutex.RLock()
880880
server, exists := s.servers[serverName]
881881
s.mutex.RUnlock()
882+
if !exists {
883+
for name, _server := range s.servers {
884+
for _, tool := range _server.Tools {
885+
if toolName == tool.Name {
886+
serverName = name
887+
server = _server
888+
exists = true
889+
break
890+
}
891+
}
892+
if exists {
893+
break
894+
}
895+
}
896+
}
882897

883898
if !exists {
884899
http.Error(w, fmt.Sprintf("Server '%s' not found", serverName), http.StatusNotFound)
@@ -1190,6 +1205,7 @@ func main() {
11901205
// Call a specific tool (old endpoint for backward compatibility)
11911206
router.HandleFunc("/tools/{server}/{tool}", service.callToolHandler).Methods("GET", "POST")
11921207
// Call a specific tool (new endpoint)
1208+
router.HandleFunc("/call/{tool}", service.callToolHandler).Methods("GET", "POST")
11931209
router.HandleFunc("/call/{server}/{tool}", service.callToolHandler).Methods("GET", "POST")
11941210

11951211
// Root endpoint with usage info
@@ -1206,8 +1222,10 @@ Available endpoints:
12061222
- GET /tools/markdown - List all tools in markdown format
12071223
- GET /tools/{server}/{tool}?param=value - Call tool with query parameters (legacy)
12081224
- GET /call/{server}/{tool}?param=value - Call tool with query parameters
1225+
- GET /call/{tool}?param=value - Call tool on auto-discovered server
12091226
- POST /tools/{server}/{tool} - Call tool with JSON body or form data (legacy)
12101227
- POST /call/{server}/{tool} - Call tool with JSON body or form data
1228+
- POST /call/{tool} - Call tool with JSON body or form data (auto-discovered server)
12111229
12121230
Examples:
12131231
- curl http://localhost:8080/tools

0 commit comments

Comments
 (0)