forked from mark3labs/mcp-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
163 lines (143 loc) · 4.02 KB
/
main.go
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
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"time"
"github.com/mark3labs/mcp-go/client"
"github.com/mark3labs/mcp-go/mcp"
)
func main() {
c, err := client.NewStdioMCPClient(
"npx",
[]string{}, // Empty ENV
"-y",
"@modelcontextprotocol/server-filesystem",
"/tmp",
)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
defer c.Close()
// Create context with timeout
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
// Initialize the client
fmt.Println("Initializing client...")
initRequest := mcp.InitializeRequest{}
initRequest.Params.ProtocolVersion = mcp.LATEST_PROTOCOL_VERSION
initRequest.Params.ClientInfo = mcp.Implementation{
Name: "example-client",
Version: "1.0.0",
}
initResult, err := c.Initialize(ctx, initRequest)
if err != nil {
log.Fatalf("Failed to initialize: %v", err)
}
fmt.Printf(
"Initialized with server: %s %s\n\n",
initResult.ServerInfo.Name,
initResult.ServerInfo.Version,
)
// List Tools
fmt.Println("Listing available tools...")
toolsRequest := mcp.ListToolsRequest{}
tools, err := c.ListTools(ctx, toolsRequest)
if err != nil {
log.Fatalf("Failed to list tools: %v", err)
}
for _, tool := range tools.Tools {
fmt.Printf("- %s: %s\n", tool.Name, tool.Description)
}
fmt.Println()
// List allowed directories
fmt.Println("Listing allowed directories...")
listDirRequest := mcp.CallToolRequest{
Request: mcp.Request{
Method: "tools/call",
},
}
listDirRequest.Params.Name = "list_allowed_directories"
result, err := c.CallTool(ctx, listDirRequest)
if err != nil {
log.Fatalf("Failed to list allowed directories: %v", err)
}
printToolResult(result)
fmt.Println()
// List /tmp
fmt.Println("Listing /tmp directory...")
listTmpRequest := mcp.CallToolRequest{}
listTmpRequest.Params.Name = "list_directory"
listTmpRequest.Params.Arguments = map[string]interface{}{
"path": "/tmp",
}
result, err = c.CallTool(ctx, listTmpRequest)
if err != nil {
log.Fatalf("Failed to list directory: %v", err)
}
printToolResult(result)
fmt.Println()
// Create mcp directory
fmt.Println("Creating /tmp/mcp directory...")
createDirRequest := mcp.CallToolRequest{}
createDirRequest.Params.Name = "create_directory"
createDirRequest.Params.Arguments = map[string]interface{}{
"path": "/tmp/mcp",
}
result, err = c.CallTool(ctx, createDirRequest)
if err != nil {
log.Fatalf("Failed to create directory: %v", err)
}
printToolResult(result)
fmt.Println()
// Create hello.txt
fmt.Println("Creating /tmp/mcp/hello.txt...")
writeFileRequest := mcp.CallToolRequest{}
writeFileRequest.Params.Name = "write_file"
writeFileRequest.Params.Arguments = map[string]interface{}{
"path": "/tmp/mcp/hello.txt",
"content": "Hello World",
}
result, err = c.CallTool(ctx, writeFileRequest)
if err != nil {
log.Fatalf("Failed to create file: %v", err)
}
printToolResult(result)
fmt.Println()
// Verify file contents
fmt.Println("Reading /tmp/mcp/hello.txt...")
readFileRequest := mcp.CallToolRequest{}
readFileRequest.Params.Name = "read_file"
readFileRequest.Params.Arguments = map[string]interface{}{
"path": "/tmp/mcp/hello.txt",
}
result, err = c.CallTool(ctx, readFileRequest)
if err != nil {
log.Fatalf("Failed to read file: %v", err)
}
printToolResult(result)
// Get file info
fmt.Println("Getting info for /tmp/mcp/hello.txt...")
fileInfoRequest := mcp.CallToolRequest{}
fileInfoRequest.Params.Name = "get_file_info"
fileInfoRequest.Params.Arguments = map[string]interface{}{
"path": "/tmp/mcp/hello.txt",
}
result, err = c.CallTool(ctx, fileInfoRequest)
if err != nil {
log.Fatalf("Failed to get file info: %v", err)
}
printToolResult(result)
}
// Helper function to print tool results
func printToolResult(result *mcp.CallToolResult) {
for _, content := range result.Content {
if textContent, ok := content.(mcp.TextContent); ok {
fmt.Println(textContent.Text)
} else {
jsonBytes, _ := json.MarshalIndent(content, "", " ")
fmt.Println(string(jsonBytes))
}
}
}