-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathstdio_client.go
More file actions
529 lines (444 loc) · 15.2 KB
/
Copy pathstdio_client.go
File metadata and controls
529 lines (444 loc) · 15.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
// Tencent is pleased to support the open source community by making trpc-mcp-go available.
//
// Copyright (C) 2025 Tencent. All rights reserved.
//
// trpc-mcp-go is licensed under the Apache License Version 2.0.
package mcp
import (
"context"
"fmt"
"sync"
"sync/atomic"
"time"
)
// StdioTransportConfig defines the complete configuration for a stdio MCP transport.
type StdioTransportConfig struct {
ServerParams StdioServerParameters `json:"server_params"`
Timeout time.Duration `json:"timeout"`
}
// Validate checks if the StdioTransportConfig is valid.
func (c StdioTransportConfig) Validate() error {
if c.ServerParams.Command == "" {
return fmt.Errorf("command cannot be empty")
}
if c.Timeout <= 0 {
return fmt.Errorf("timeout must be positive")
}
return nil
}
// StdioClient represents a specialized MCP client for stdio-based servers.
// It implements both Connector and ProcessClient interfaces.
type StdioClient struct {
transport *stdioClientTransport
clientInfo Implementation
protocolVersion string
initialized atomic.Bool
requestID atomic.Int64
capabilities map[string]interface{}
state atomic.Value // stores State
logger Logger
// Roots support.
rootsProvider RootsProvider // Provider for roots information.
rootsMu sync.RWMutex // Mutex for protecting the rootsProvider.
// Whether to include "arguments": {} for tool calls with no arguments.
sendEmptyToolArguments bool
}
// StdioClientOption defines configuration options for StdioClient.
type StdioClientOption func(*StdioClient)
// NewStdioClient creates a new stdio-based MCP client
func NewStdioClient(config StdioTransportConfig, clientInfo Implementation, options ...StdioClientOption) (*StdioClient, error) {
// Validate configuration.
if err := config.Validate(); err != nil {
return nil, fmt.Errorf("invalid configuration: %w", err)
}
// Create client.
client := &StdioClient{
clientInfo: clientInfo,
protocolVersion: ProtocolVersion_2025_03_26,
capabilities: make(map[string]interface{}),
logger: GetDefaultLogger(),
}
// Set initial state.
client.state.Store(StateDisconnected)
// Apply options.
for _, option := range options {
option(client)
}
// Create transport options.
var transportOptions []stdioTransportOption
if config.Timeout > 0 {
transportOptions = append(transportOptions, withStdioTransportTimeout(config.Timeout))
}
if client.logger != nil {
transportOptions = append(transportOptions, withStdioTransportLogger(client.logger))
}
// Create transport.
client.transport = newStdioClientTransport(config.ServerParams, transportOptions...)
// Set client reference in transport for roots handling.
client.transport.client = client
return client, nil
}
// WithStdioLogger sets the logger for the client.
func WithStdioLogger(logger Logger) StdioClientOption {
return func(c *StdioClient) {
c.logger = logger
}
}
// WithStdioProtocolVersion sets the protocol version.
func WithStdioProtocolVersion(version string) StdioClientOption {
return func(c *StdioClient) {
c.protocolVersion = version
}
}
// WithStdioCapabilities sets client capabilities.
func WithStdioCapabilities(capabilities map[string]interface{}) StdioClientOption {
return func(c *StdioClient) {
for k, v := range capabilities {
c.capabilities[k] = v
}
}
}
// WithStdioSendEmptyToolArguments sets whether CallTool sends "arguments": {}
// when no tool arguments are provided.
func WithStdioSendEmptyToolArguments(enabled bool) StdioClientOption {
return func(c *StdioClient) {
c.sendEmptyToolArguments = enabled
}
}
// Initialize initializes the client connection
func (c *StdioClient) Initialize(ctx context.Context, req *InitializeRequest) (*InitializeResult, error) {
if c.initialized.Load() {
return nil, fmt.Errorf("client already initialized")
}
// Create initialization request
requestID := c.requestID.Add(1)
jsonReq := newJSONRPCRequest(requestID, MethodInitialize, map[string]interface{}{
"protocolVersion": c.protocolVersion,
"clientInfo": c.clientInfo,
"capabilities": c.capabilities,
})
// Override with provided params if any.
if req != nil && !isZeroStruct(req.Params) {
jsonReq.Params = req.Params
}
// Send request
rawResp, err := c.transport.sendRequest(ctx, jsonReq)
if err != nil {
c.setState(StateDisconnected)
return nil, fmt.Errorf("initialization failed: %w", err)
}
// Update state
c.setState(StateConnected)
// Check for error response
if isErrorResponse(rawResp) {
errResp, err := parseRawMessageToError(rawResp)
if err != nil {
c.setState(StateDisconnected)
return nil, fmt.Errorf("failed to parse error response: %w", err)
}
c.setState(StateDisconnected)
return nil, fmt.Errorf("initialization error: %s (code: %d)",
errResp.Error.Message, errResp.Error.Code)
}
// Parse response
initResult, err := parseInitializeResultFromJSON(rawResp)
if err != nil {
c.setState(StateDisconnected)
return nil, fmt.Errorf("failed to parse initialization response: %w", err)
}
// Send initialized notification
if err := c.sendInitialized(ctx); err != nil {
c.setState(StateDisconnected)
return nil, fmt.Errorf("failed to send initialized notification: %w", err)
}
// Mark as initialized and update state.
c.initialized.Store(true)
c.setState(StateInitialized)
return initResult, nil
}
// sendInitialized sends the initialized notification
func (c *StdioClient) sendInitialized(ctx context.Context) error {
notification := NewInitializedNotification()
return c.transport.sendNotification(ctx, notification)
}
// Close closes the client and terminates the process.
func (c *StdioClient) Close() error {
if c.transport != nil {
err := c.transport.close()
c.setState(StateDisconnected)
c.initialized.Store(false)
return err
}
return nil
}
// GetState returns the current client state.
func (c *StdioClient) GetState() State {
if state := c.state.Load(); state != nil {
return state.(State)
}
return StateDisconnected
}
// setState sets the client state thread-safely.
func (c *StdioClient) setState(state State) {
c.state.Store(state)
}
// ListTools lists available tools.
func (c *StdioClient) ListTools(ctx context.Context, req *ListToolsRequest) (*ListToolsResult, error) {
if !c.initialized.Load() {
return nil, fmt.Errorf("client not initialized")
}
requestID := c.requestID.Add(1)
jsonReq := &JSONRPCRequest{
JSONRPC: JSONRPCVersion,
ID: requestID,
Request: Request{
Method: MethodToolsList,
},
Params: req.Params,
}
rawResp, err := c.transport.sendRequest(ctx, jsonReq)
if err != nil {
return nil, fmt.Errorf("list tools request failed: %w", err)
}
if isErrorResponse(rawResp) {
errResp, err := parseRawMessageToError(rawResp)
if err != nil {
return nil, fmt.Errorf("failed to parse error response: %w", err)
}
return nil, fmt.Errorf("list tools error: %s (code: %d)",
errResp.Error.Message, errResp.Error.Code)
}
return parseListToolsResultFromJSON(rawResp)
}
// CallTool calls a specific tool.
func (c *StdioClient) CallTool(ctx context.Context, req *CallToolRequest) (*CallToolResult, error) {
if !c.initialized.Load() {
return nil, fmt.Errorf("client not initialized")
}
requestID := c.requestID.Add(1)
params := map[string]interface{}{
"name": req.Params.Name,
"arguments": req.Params.Arguments,
}
if c.sendEmptyToolArguments && len(req.Params.Arguments) == 0 {
params["arguments"] = map[string]interface{}{}
}
jsonReq := newJSONRPCRequest(requestID, MethodToolsCall, params)
rawResp, err := c.transport.sendRequest(ctx, jsonReq)
if err != nil {
return nil, fmt.Errorf("call tool request failed: %w", err)
}
if isErrorResponse(rawResp) {
errResp, err := parseRawMessageToError(rawResp)
if err != nil {
return nil, fmt.Errorf("failed to parse error response: %w", err)
}
return nil, fmt.Errorf("call tool error: %s (code: %d)",
errResp.Error.Message, errResp.Error.Code)
}
return parseCallToolResult(rawResp)
}
// ListPrompts lists available prompts.
func (c *StdioClient) ListPrompts(ctx context.Context, req *ListPromptsRequest) (*ListPromptsResult, error) {
if !c.initialized.Load() {
return nil, fmt.Errorf("client not initialized")
}
requestID := c.requestID.Add(1)
jsonReq := &JSONRPCRequest{
JSONRPC: JSONRPCVersion,
ID: requestID,
Request: Request{
Method: MethodPromptsList,
},
Params: req.Params,
}
rawResp, err := c.transport.sendRequest(ctx, jsonReq)
if err != nil {
return nil, fmt.Errorf("list prompts request failed: %w", err)
}
if isErrorResponse(rawResp) {
errResp, err := parseRawMessageToError(rawResp)
if err != nil {
return nil, fmt.Errorf("failed to parse error response: %w", err)
}
return nil, fmt.Errorf("list prompts error: %s (code: %d)",
errResp.Error.Message, errResp.Error.Code)
}
return parseListPromptsResultFromJSON(rawResp)
}
// GetPrompt gets a specific prompt.
func (c *StdioClient) GetPrompt(ctx context.Context, req *GetPromptRequest) (*GetPromptResult, error) {
if !c.initialized.Load() {
return nil, fmt.Errorf("client not initialized")
}
requestID := c.requestID.Add(1)
jsonReq := newJSONRPCRequest(requestID, MethodPromptsGet, map[string]interface{}{
"name": req.Params.Name,
"arguments": req.Params.Arguments,
})
rawResp, err := c.transport.sendRequest(ctx, jsonReq)
if err != nil {
return nil, fmt.Errorf("get prompt request failed: %w", err)
}
if isErrorResponse(rawResp) {
errResp, err := parseRawMessageToError(rawResp)
if err != nil {
return nil, fmt.Errorf("failed to parse error response: %w", err)
}
return nil, fmt.Errorf("get prompt error: %s (code: %d)",
errResp.Error.Message, errResp.Error.Code)
}
return parseGetPromptResultFromJSON(rawResp)
}
// ListResources lists available resources.
func (c *StdioClient) ListResources(ctx context.Context, req *ListResourcesRequest) (*ListResourcesResult, error) {
if !c.initialized.Load() {
return nil, fmt.Errorf("client not initialized")
}
requestID := c.requestID.Add(1)
jsonReq := &JSONRPCRequest{
JSONRPC: JSONRPCVersion,
ID: requestID,
Request: Request{
Method: MethodResourcesList,
},
Params: req.Params,
}
rawResp, err := c.transport.sendRequest(ctx, jsonReq)
if err != nil {
return nil, fmt.Errorf("list resources request failed: %w", err)
}
if isErrorResponse(rawResp) {
errResp, err := parseRawMessageToError(rawResp)
if err != nil {
return nil, fmt.Errorf("failed to parse error response: %w", err)
}
return nil, fmt.Errorf("list resources error: %s (code: %d)",
errResp.Error.Message, errResp.Error.Code)
}
return parseListResourcesResultFromJSON(rawResp)
}
// ReadResource reads a specific resource.
func (c *StdioClient) ReadResource(ctx context.Context, req *ReadResourceRequest) (*ReadResourceResult, error) {
if !c.initialized.Load() {
return nil, fmt.Errorf("client not initialized")
}
requestID := c.requestID.Add(1)
jsonReq := newJSONRPCRequest(requestID, MethodResourcesRead, map[string]interface{}{
"uri": req.Params.URI,
"arguments": req.Params.Arguments,
})
rawResp, err := c.transport.sendRequest(ctx, jsonReq)
if err != nil {
return nil, fmt.Errorf("read resource request failed: %w", err)
}
if isErrorResponse(rawResp) {
errResp, err := parseRawMessageToError(rawResp)
if err != nil {
return nil, fmt.Errorf("failed to parse error response: %w", err)
}
return nil, fmt.Errorf("read resource error: %s (code: %d)",
errResp.Error.Message, errResp.Error.Code)
}
return parseReadResourceResultFromJSON(rawResp)
}
// RegisterNotificationHandler registers a notification handler.
func (c *StdioClient) RegisterNotificationHandler(method string, handler NotificationHandler) {
c.transport.registerNotificationHandler(method, handler)
}
// UnregisterNotificationHandler unregisters a notification handler.
func (c *StdioClient) UnregisterNotificationHandler(method string) {
c.transport.unregisterNotificationHandler(method)
}
// GetProcessID returns the process ID.
func (c *StdioClient) GetProcessID() int {
return c.transport.getProcessID()
}
// GetCommandLine returns the command line.
func (c *StdioClient) GetCommandLine() []string {
return c.transport.getCommandLine()
}
// IsProcessRunning checks if the process is running.
func (c *StdioClient) IsProcessRunning() bool {
return c.transport.isProcessRunning()
}
// RestartProcess restarts the server process.
func (c *StdioClient) RestartProcess(ctx context.Context) error {
// Close current process
if err := c.transport.close(); err != nil {
c.logger.Warnf("Error closing current process: %v", err)
}
// Reset state.
c.initialized.Store(false)
c.setState(StateDisconnected)
// Start new process
if err := c.transport.startProcess(); err != nil {
return fmt.Errorf("failed to restart process: %w", err)
}
return nil
}
// GetTransportInfo returns information about the transport.
func (c *StdioClient) GetTransportInfo() TransportInfo {
capabilities := make(map[string]interface{})
capabilities["process_management"] = true
capabilities["command_line"] = c.GetCommandLine()
capabilities["working_directory"] = c.transport.serverParams.WorkingDir
if len(c.transport.serverParams.Env) > 0 {
capabilities["environment_variables"] = c.transport.serverParams.Env
}
return TransportInfo{
Type: "stdio",
Description: "Standard Input/Output transport with process management",
Capabilities: capabilities,
}
}
// SetRootsProvider sets the provider for responding to server's roots/list requests.
func (c *StdioClient) SetRootsProvider(provider RootsProvider) {
c.rootsMu.Lock()
defer c.rootsMu.Unlock()
c.rootsProvider = provider
}
// SendRootsListChangedNotification notifies server that roots changed.
func (c *StdioClient) SendRootsListChangedNotification(ctx context.Context) error {
// Create roots list changed notification.
notification := &JSONRPCNotification{
JSONRPC: JSONRPCVersion,
Notification: Notification{
Method: MethodNotificationsRootsListChanged,
},
}
return c.transport.sendNotification(ctx, notification)
}
// NewNpxStdioClient creates a new stdio client for NPX-based servers.
func NewNpxStdioClient(packageName string, args []string, clientInfo Implementation, options ...StdioClientOption) (*StdioClient, error) {
config := StdioTransportConfig{
ServerParams: StdioServerParameters{
Command: "npx",
Args: append([]string{"-y", packageName}, args...),
},
Timeout: 30 * time.Second,
}
return NewStdioClient(config, clientInfo, options...)
}
// NewPythonStdioClient creates a new stdio client for Python-based servers.
func NewPythonStdioClient(scriptPath string, args []string, clientInfo Implementation, options ...StdioClientOption) (*StdioClient, error) {
config := StdioTransportConfig{
ServerParams: StdioServerParameters{
Command: "python",
Args: append([]string{scriptPath}, args...),
},
Timeout: 30 * time.Second,
}
return NewStdioClient(config, clientInfo, options...)
}
// NewNodeStdioClient creates a new stdio client for Node.js-based servers.
func NewNodeStdioClient(scriptPath string, args []string, clientInfo Implementation, options ...StdioClientOption) (*StdioClient, error) {
config := StdioTransportConfig{
ServerParams: StdioServerParameters{
Command: "node",
Args: append([]string{scriptPath}, args...),
},
Timeout: 30 * time.Second,
}
return NewStdioClient(config, clientInfo, options...)
}