forked from Andrea-Cavallo/adOmnia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmcp_bindings.go
More file actions
244 lines (215 loc) · 5.64 KB
/
Copy pathmcp_bindings.go
File metadata and controls
244 lines (215 loc) · 5.64 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
package main
import (
"adomnia/internal/mcp"
"context"
"encoding/json"
"fmt"
"sync"
)
const defaultMCPSessionID = "default"
type mcpSession struct {
client *mcp.Client
cfg mcp.ConnectionConfig
}
type MCPClient struct {
mu sync.RWMutex
sessions map[string]*mcpSession
}
func NewMCPClient() *MCPClient {
return &MCPClient{sessions: make(map[string]*mcpSession)}
}
func (m *MCPClient) ConnectSession(sessionID, cfgJSON string) (string, error) {
if sessionID == "" {
return "", fmt.Errorf("session ID is required")
}
var cfg mcp.ConnectionConfig
if err := json.Unmarshal([]byte(cfgJSON), &cfg); err != nil {
return "", fmt.Errorf("invalid config: %w", err)
}
c, err := mcp.NewClient(cfg)
if err != nil {
return "", err
}
result, err := c.Initialize(context.Background())
if err != nil {
_ = c.Close()
return "", err
}
m.mu.Lock()
if existing, ok := m.sessions[sessionID]; ok {
_ = existing.client.Close()
}
m.sessions[sessionID] = &mcpSession{client: c, cfg: cfg}
m.mu.Unlock()
raw, _ := json.Marshal(result)
return string(raw), nil
}
func (m *MCPClient) DisconnectSession(sessionID string) error {
m.mu.Lock()
defer m.mu.Unlock()
session, ok := m.sessions[sessionID]
if !ok {
return nil
}
err := session.client.Close()
delete(m.sessions, sessionID)
return err
}
func (m *MCPClient) GetSessionStatus(sessionID string) string {
m.mu.RLock()
session, ok := m.sessions[sessionID]
m.mu.RUnlock()
if !ok {
return "disconnected"
}
return sessionStatus(session)
}
func (m *MCPClient) RestartSession(sessionID string) (string, error) {
m.mu.RLock()
session, ok := m.sessions[sessionID]
if !ok {
m.mu.RUnlock()
return "", fmt.Errorf("session %s not found", sessionID)
}
cfg := session.cfg
m.mu.RUnlock()
_ = m.DisconnectSession(sessionID)
cfgRaw, _ := json.Marshal(cfg)
return m.ConnectSession(sessionID, string(cfgRaw))
}
func (m *MCPClient) ListSessions() string {
type sessionInfo struct {
ID string `json:"id"`
Transport string `json:"transport"`
Status string `json:"status"`
}
m.mu.RLock()
items := make([]sessionInfo, 0, len(m.sessions))
for id, session := range m.sessions {
items = append(items, sessionInfo{
ID: id,
Transport: session.client.Transport(),
Status: sessionStatus(session),
})
}
m.mu.RUnlock()
raw, _ := json.Marshal(items)
return string(raw)
}
func (m *MCPClient) Connect(cfgJSON string) (string, error) {
return m.ConnectSession(defaultMCPSessionID, cfgJSON)
}
func (m *MCPClient) Disconnect() error {
return m.DisconnectSession(defaultMCPSessionID)
}
func (m *MCPClient) getSessionClient(sessionID string) *mcp.Client {
m.mu.RLock()
defer m.mu.RUnlock()
if session, ok := m.sessions[sessionID]; ok {
return session.client
}
return nil
}
func (m *MCPClient) ListTools() (string, error) {
return m.ListToolsSession(defaultMCPSessionID)
}
func (m *MCPClient) ListToolsSession(sessionID string) (string, error) {
c := m.getSessionClient(sessionID)
if c == nil {
return "", fmt.Errorf("not connected")
}
tools, err := c.ListTools(context.Background())
if err != nil {
return "", err
}
raw, _ := json.Marshal(tools)
return string(raw), nil
}
func (m *MCPClient) CallTool(name, argsJSON string) (string, error) {
return m.CallToolSession(defaultMCPSessionID, name, argsJSON)
}
func (m *MCPClient) CallToolSession(sessionID, name, argsJSON string) (string, error) {
c := m.getSessionClient(sessionID)
if c == nil {
return "", fmt.Errorf("not connected")
}
var args map[string]any
if argsJSON != "" {
if err := json.Unmarshal([]byte(argsJSON), &args); err != nil {
return "", fmt.Errorf("invalid args JSON: %w", err)
}
}
result, err := c.CallTool(context.Background(), name, args)
if err != nil {
return "", err
}
raw, _ := json.Marshal(result)
return string(raw), nil
}
func (m *MCPClient) ListResources() (string, error) {
return m.ListResourcesSession(defaultMCPSessionID)
}
func (m *MCPClient) ListResourcesSession(sessionID string) (string, error) {
c := m.getSessionClient(sessionID)
if c == nil {
return "", fmt.Errorf("not connected")
}
resources, err := c.ListResources(context.Background())
if err != nil {
return "", err
}
raw, _ := json.Marshal(resources)
return string(raw), nil
}
func (m *MCPClient) ListPrompts() (string, error) {
return m.ListPromptsSession(defaultMCPSessionID)
}
func (m *MCPClient) ListPromptsSession(sessionID string) (string, error) {
c := m.getSessionClient(sessionID)
if c == nil {
return "", fmt.Errorf("not connected")
}
prompts, err := c.ListPrompts(context.Background())
if err != nil {
return "", err
}
raw, _ := json.Marshal(prompts)
return string(raw), nil
}
func (m *MCPClient) GetPrompt(name, argsJSON string) (string, error) {
return m.GetPromptSession(defaultMCPSessionID, name, argsJSON)
}
func (m *MCPClient) GetPromptSession(sessionID, name, argsJSON string) (string, error) {
c := m.getSessionClient(sessionID)
if c == nil {
return "", fmt.Errorf("not connected")
}
var args map[string]any
if argsJSON != "" {
if err := json.Unmarshal([]byte(argsJSON), &args); err != nil {
return "", fmt.Errorf("invalid args JSON: %w", err)
}
}
result, err := c.GetPrompt(context.Background(), name, args)
if err != nil {
return "", err
}
raw, _ := json.Marshal(result)
return string(raw), nil
}
func sessionStatus(session *mcpSession) string {
if session == nil || session.client == nil {
return "disconnected"
}
switch state := session.client.ProcessState(); state {
case "running":
return "connected"
case "stopped", "crashed":
return state
default:
if session.client.Transport() == string(mcp.TransportHTTP) {
return "connected"
}
return "unknown"
}
}