Skip to content

Commit be9260e

Browse files
committed
fix: address issues with nested binary calls in tests
This change also fixes an issue where the MCP client is initialized twice. Signed-off-by: Donnie Adams <[email protected]>
1 parent dc2229e commit be9260e

File tree

8 files changed

+39
-38
lines changed

8 files changed

+39
-38
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/bin
2+
/pkg/tests/bin
23
/.idea
34
/static/ui
45
**/node_modules/

go.mod

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ module github.com/gptscript-ai/gptscript
22

33
go 1.24.2
44

5+
toolchain go1.24.4
6+
7+
replace github.com/nanobot-ai/nanobot => github.com/thedadams/nanobot v0.0.0-20250612152714-166ea7e8fb71
8+
59
require (
610
github.com/AlecAivazis/survey/v2 v2.3.7
711
github.com/BurntSushi/locker v0.0.0-20171006230638-a6e239ea1c69
@@ -17,7 +21,7 @@ require (
1721
github.com/google/uuid v1.6.0
1822
github.com/gptscript-ai/broadcaster v0.0.0-20240625175512-c43682019b86
1923
github.com/gptscript-ai/chat-completion-client v0.0.0-20250224164718-139cb4507b1d
20-
github.com/gptscript-ai/cmd v0.0.0-20240802230653-326b7baf6fcb
24+
github.com/gptscript-ai/cmd v0.0.0-20250530150401-bc71fddf8070
2125
github.com/gptscript-ai/go-gptscript v0.9.6-0.20250520154649-f1616a06f1b0
2226
github.com/gptscript-ai/tui v0.0.0-20250419050840-5e79e16786c9
2327
github.com/hexops/autogold/v2 v2.2.1

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,8 @@ github.com/gptscript-ai/broadcaster v0.0.0-20240625175512-c43682019b86 h1:m9yLtI
213213
github.com/gptscript-ai/broadcaster v0.0.0-20240625175512-c43682019b86/go.mod h1:lK3K5EZx4dyT24UG3yCt0wmspkYqrj4D/8kxdN3relk=
214214
github.com/gptscript-ai/chat-completion-client v0.0.0-20250224164718-139cb4507b1d h1:p5uqZufDIMQzAALblZFkr8fwbnZbFXbBCR1ZMAFylXk=
215215
github.com/gptscript-ai/chat-completion-client v0.0.0-20250224164718-139cb4507b1d/go.mod h1:7P/o6/IWa1KqsntVf68hSnLKuu3+xuqm6lYhch1w4jo=
216-
github.com/gptscript-ai/cmd v0.0.0-20240802230653-326b7baf6fcb h1:ky2J2CzBOskC7Jgm2VJAQi2x3p7FVGa+2/PcywkFJuc=
217-
github.com/gptscript-ai/cmd v0.0.0-20240802230653-326b7baf6fcb/go.mod h1:DJAo1xTht1LDkNYFNydVjTHd576TC7MlpsVRl3oloVw=
216+
github.com/gptscript-ai/cmd v0.0.0-20250530150401-bc71fddf8070 h1:xm5ZZFraWFwxyE7TBEncCXArubCDZTwG6s5bpMzqhSY=
217+
github.com/gptscript-ai/cmd v0.0.0-20250530150401-bc71fddf8070/go.mod h1:DJAo1xTht1LDkNYFNydVjTHd576TC7MlpsVRl3oloVw=
218218
github.com/gptscript-ai/go-gptscript v0.9.6-0.20250520154649-f1616a06f1b0 h1:UXZRFAUPDWOgeTyjZd4M8YrEEgPc7XOfjgbm81w7x0w=
219219
github.com/gptscript-ai/go-gptscript v0.9.6-0.20250520154649-f1616a06f1b0/go.mod h1:t2TyiEa6rhd4reOcorAMUmd5MledmZuTmYrO7rV3Iy8=
220220
github.com/gptscript-ai/tui v0.0.0-20250419050840-5e79e16786c9 h1:wQC8sKyeGA50WnCEG+Jo5FNRIkuX3HX8d3ubyWCCoI8=
@@ -311,8 +311,6 @@ github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s=
311311
github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8=
312312
github.com/muesli/termenv v0.16.0 h1:S5AlUN9dENB57rsbnkPyfdGuWIlkmzJjbFf0Tf5FWUc=
313313
github.com/muesli/termenv v0.16.0/go.mod h1:ZRfOIKPFDYQoDFF4Olj7/QJbW60Ol/kL1pU3VfY/Cnk=
314-
github.com/nanobot-ai/nanobot v0.0.6-0.20250612011942-bda908a3e3ab h1:ZYLDoc5w3iqb7ykbslG+Fr0uCA8wMTWWblT27cerwzg=
315-
github.com/nanobot-ai/nanobot v0.0.6-0.20250612011942-bda908a3e3ab/go.mod h1:XAvQcMgztKKR8Ul7/i28MfepoyC72ZGwG3uzAIH9F6c=
316314
github.com/nightlyone/lockfile v1.0.0 h1:RHep2cFKK4PonZJDdEl4GmkabuhbsRMgk/k3uAmxBiA=
317315
github.com/nightlyone/lockfile v1.0.0/go.mod h1:rywoIealpdNse2r832aiD9jRk8ErCatROs6LzC841CI=
318316
github.com/nwaples/rardecode/v2 v2.0.0-beta.4.0.20241112120701-034e449c6e78 h1:MYzLheyVx1tJVDqfu3YnN4jtnyALNzLvwl+f58TcvQY=
@@ -398,6 +396,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
398396
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
399397
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
400398
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
399+
github.com/thedadams/nanobot v0.0.0-20250612152714-166ea7e8fb71 h1:Y4ysr+aVjLQEcpWRkwZ+NioEtO5oaiNuT6QcnvydmVY=
400+
github.com/thedadams/nanobot v0.0.0-20250612152714-166ea7e8fb71/go.mod h1:XAvQcMgztKKR8Ul7/i28MfepoyC72ZGwG3uzAIH9F6c=
401401
github.com/therootcompany/xz v1.0.1 h1:CmOtsn1CbtmyYiusbfmhmkpAAETj0wBIH6kCYaX+xzw=
402402
github.com/therootcompany/xz v1.0.1/go.mod h1:3K3UH1yCKgBneZYhuQUvJ9HPD19UEXEI0BWbMn8qNMY=
403403
github.com/tidwall/gjson v1.17.1 h1:wlYEnwqAHgzmhNUFfw7Xalt2JzQvsMx2Se4PcoFCT/U=

pkg/mcp/client.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,14 @@ func (l *Local) Client(server ServerConfig) (*Client, error) {
1111
}
1212

1313
return &Client{
14-
Client: session.Client,
15-
InitResult: session.InitResult,
14+
Client: session.Client,
1615
}, nil
1716
}
1817

1918
type Client struct {
2019
*nmcp.Client
21-
InitResult nmcp.InitializeResult
2220
}
2321

2422
func (c *Client) Capabilities() nmcp.ServerCapabilities {
25-
return c.InitResult.Capabilities
23+
return c.Client.Session.InitializeResult.Capabilities
2624
}

pkg/mcp/loader.go

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import (
1414
"github.com/gptscript-ai/gptscript/pkg/hash"
1515
"github.com/gptscript-ai/gptscript/pkg/mvl"
1616
"github.com/gptscript-ai/gptscript/pkg/types"
17-
"github.com/gptscript-ai/gptscript/pkg/version"
1817
nmcp "github.com/nanobot-ai/nanobot/pkg/mcp"
1918
)
2019

@@ -33,10 +32,9 @@ type Local struct {
3332
}
3433

3534
type Session struct {
36-
ID string
37-
InitResult nmcp.InitializeResult
38-
Client *nmcp.Client
39-
Config ServerConfig
35+
ID string
36+
Client *nmcp.Client
37+
Config ServerConfig
4038
}
4139

4240
type Config struct {
@@ -145,6 +143,7 @@ func (l *Local) ShutdownServer(server ServerConfig) error {
145143

146144
if session != nil && session.Client != nil {
147145
session.Client.Session.Close()
146+
session.Client.Session.Wait()
148147
}
149148

150149
return nil
@@ -224,7 +223,7 @@ func (l *Local) sessionToTools(ctx context.Context, session *Session, toolName s
224223
},
225224
}
226225

227-
if string(annotations) != "{}" {
226+
if string(annotations) != "{}" && string(annotations) != "null" {
228227
toolDef.MetaData = map[string]string{
229228
"mcp-tool-annotations": string(annotations),
230229
}
@@ -243,7 +242,7 @@ func (l *Local) sessionToTools(ctx context.Context, session *Session, toolName s
243242
ToolDef: types.ToolDef{
244243
Parameters: types.Parameters{
245244
Name: toolName,
246-
Description: session.InitResult.ServerInfo.Name,
245+
Description: session.Client.Session.InitializeResult.ServerInfo.Name,
247246
Export: toolNames,
248247
},
249248
MetaData: map[string]string{
@@ -252,18 +251,18 @@ func (l *Local) sessionToTools(ctx context.Context, session *Session, toolName s
252251
},
253252
}
254253

255-
if session.InitResult.Instructions != "" {
254+
if session.Client.Session.InitializeResult.Instructions != "" {
256255
data, _ := json.Marshal(map[string]any{
257256
"tools": toolNames,
258-
"instructions": session.InitResult.Instructions,
257+
"instructions": session.Client.Session.InitializeResult.Instructions,
259258
})
260259
toolDefs = append(toolDefs, types.Tool{
261260
ToolDef: types.ToolDef{
262261
Parameters: types.Parameters{
263262
Name: session.ID,
264263
Type: "context",
265264
},
266-
Instructions: types.EchoPrefix + "\n" + `# START MCP SERVER INFO: ` + session.InitResult.ServerInfo.Name + "\n" +
265+
Instructions: types.EchoPrefix + "\n" + `# START MCP SERVER INFO: ` + session.Client.Session.InitializeResult.ServerInfo.Name + "\n" +
267266
`You have available the following tools from an MCP Server that has provided the following additional instructions` + "\n" +
268267
string(data) + "\n" +
269268
`# END MCP SERVER INFO` + "\n",
@@ -284,7 +283,6 @@ func (l *Local) loadSession(server ServerConfig, serverName string) (*Session, e
284283
if l.sessionCtx == nil {
285284
l.sessionCtx, l.cancel = context.WithCancel(context.Background())
286285
}
287-
ctx := l.sessionCtx
288286
l.lock.Unlock()
289287

290288
if ok {
@@ -303,22 +301,10 @@ func (l *Local) loadSession(server ServerConfig, serverName string) (*Session, e
303301
return nil, fmt.Errorf("failed to create MCP stdio client: %w", err)
304302
}
305303

306-
initResult, err := c.Initialize(ctx, nmcp.InitializeRequest{
307-
ProtocolVersion: "2025-03-26",
308-
ClientInfo: nmcp.ClientInfo{
309-
Name: version.ProgramName,
310-
Version: version.Get().String(),
311-
},
312-
})
313-
if err != nil {
314-
return nil, fmt.Errorf("failed to initialize MCP client: %w", err)
315-
}
316-
317304
result := &Session{
318-
ID: id,
319-
InitResult: initResult,
320-
Client: c,
321-
Config: server,
305+
ID: id,
306+
Client: c,
307+
Config: server,
322308
}
323309

324310
l.lock.Lock()
@@ -330,7 +316,7 @@ func (l *Local) loadSession(server ServerConfig, serverName string) (*Session, e
330316
}
331317

332318
if l.sessions == nil {
333-
l.sessions = make(map[string]*Session)
319+
l.sessions = make(map[string]*Session, 1)
334320
}
335321
l.sessions[id] = result
336322
return result, nil

pkg/tests/runner2_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ func TestMCPLoad(t *testing.T) {
212212
}
213213

214214
r := tester.NewRunner(t)
215-
prg, err := loader.ProgramFromSource(context.Background(), `
215+
prg, err := loader.ProgramFromSource(t.Context(), `
216216
name: mcp
217217
218218
#!mcp

pkg/tests/runner_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"encoding/json"
88
"io"
99
"os"
10+
"os/exec"
1011
"runtime"
1112
"testing"
1213

@@ -27,6 +28,17 @@ func toJSONString(t *testing.T, v interface{}) string {
2728
return string(x)
2829
}
2930

31+
func TestMain(m *testing.M) {
32+
cmd := exec.CommandContext(context.Background(), "go", "build", "-o", "bin/gptscript", "../../main.go")
33+
if err := cmd.Run(); err != nil {
34+
panic(err)
35+
}
36+
37+
os.Setenv("NANOBOT_BIN", "bin/gptscript")
38+
defer os.Unsetenv("NANOBOT_BIN")
39+
m.Run()
40+
}
41+
3042
func TestAsterick(t *testing.T) {
3143
r := tester.NewRunner(t)
3244
p, err := r.Load("")
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
`{
22
"done": true,
3-
"content": "{\"content\":[{\"type\":\"text\",\"text\":\"[{'1': 1}]\"}]}",
3+
"content": "{\"isError\":false,\"content\":[{\"type\":\"text\",\"text\":\"[{'1': 1}]\"}]}",
44
"toolID": "",
55
"state": null
66
}`

0 commit comments

Comments
 (0)