-
-
Notifications
You must be signed in to change notification settings - Fork 171
Expand file tree
/
Copy pathmcp.go
More file actions
239 lines (212 loc) · 9.26 KB
/
mcp.go
File metadata and controls
239 lines (212 loc) · 9.26 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
package config
import (
"time"
"github.com/ifuryst/lol"
"github.com/amoylab/unla/internal/common/cnst"
"github.com/amoylab/unla/pkg/mcp"
)
type (
// MCPServer represents the MCP server data structure
MCPServer struct {
Name string `json:"name" yaml:"name"`
Content MCPConfig `json:"content" yaml:"content"`
CreatedAt time.Time `json:"createdAt" yaml:"createdAt"`
UpdatedAt time.Time `json:"updatedAt" yaml:"updatedAt"`
}
MCPConfig struct {
Name string `json:"name" yaml:"name"`
Tenant string `json:"tenant" yaml:"tenant"`
CreatedAt time.Time `json:"createdAt" yaml:"createdAt"`
UpdatedAt time.Time `json:"updatedAt" yaml:"updatedAt"`
DeletedAt time.Time `json:"deletedAt,omitempty" yaml:"deletedAt,omitempty"` // non-zero indicates that all information has been deleted
Routers []RouterConfig `json:"routers,omitempty" yaml:"routers,omitempty"`
Servers []ServerConfig `json:"servers,omitempty" yaml:"servers,omitempty"`
Tools []ToolConfig `json:"tools,omitempty" yaml:"tools,omitempty"`
Prompts []PromptConfig `json:"prompts,omitempty" yaml:"prompts,omitempty"`
McpServers []MCPServerConfig `json:"mcpServers,omitempty" yaml:"mcpServers,omitempty"` // proxy mcp servers
}
RouterConfig struct {
Server string `json:"server" yaml:"server"`
Prefix string `json:"prefix" yaml:"prefix"`
SSEPrefix string `json:"ssePrefix" yaml:"ssePrefix"`
CORS *CORSConfig `json:"cors,omitempty" yaml:"cors,omitempty"`
Auth *Auth `json:"auth,omitempty" yaml:"auth,omitempty"`
}
CORSConfig struct {
AllowOrigins []string `json:"allowOrigins,omitempty" yaml:"allowOrigins,omitempty"`
AllowMethods []string `json:"allowMethods,omitempty" yaml:"allowMethods,omitempty"`
AllowHeaders []string `json:"allowHeaders,omitempty" yaml:"allowHeaders,omitempty"`
ExposeHeaders []string `json:"exposeHeaders,omitempty" yaml:"exposeHeaders,omitempty"`
AllowCredentials bool `json:"allowCredentials" yaml:"allowCredentials"`
}
ProxyConfig struct {
Host string `json:"host" yaml:"host"`
Port int `json:"port" yaml:"port"`
Type string `json:"type" yaml:"type"` // http, https, socks5
}
ServerConfig struct {
Name string `json:"name" yaml:"name"`
Description string `json:"description" yaml:"description"`
AllowedTools []string `json:"allowedTools,omitempty" yaml:"allowedTools,omitempty"`
Config map[string]string `json:"config,omitempty" yaml:"config,omitempty"`
}
ToolConfig struct {
Name string `json:"name" yaml:"name"`
Title string `json:"title,omitempty" yaml:"title,omitempty"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
Method string `json:"method" yaml:"method"`
Endpoint string `json:"endpoint" yaml:"endpoint"`
Proxy *ProxyConfig `json:"proxy,omitempty" yaml:"proxy,omitempty"`
Headers map[string]string `json:"headers,omitempty" yaml:"headers,omitempty"`
Args []ArgConfig `json:"args,omitempty" yaml:"args,omitempty"`
RequestBody string `json:"requestBody" yaml:"requestBody"`
ResponseBody string `json:"responseBody" yaml:"responseBody"`
InputSchema map[string]any `json:"inputSchema,omitempty" yaml:"inputSchema,omitempty"`
OutputSchema map[string]any `json:"outputSchema,omitempty" yaml:"outputSchema,omitempty"`
}
MCPServerConfig struct {
Type string `json:"type" yaml:"type"` // sse, stdio and streamable-http
Name string `json:"name" yaml:"name"` // server name
Command string `json:"command,omitempty" yaml:"command,omitempty"` // for stdio
Args []string `json:"args,omitempty" yaml:"args,omitempty"` // for stdio
Env map[string]string `json:"env,omitempty" yaml:"env,omitempty"` // for stdio
URL string `json:"url,omitempty" yaml:"url,omitempty"` // for sse and streamable-http
Policy cnst.MCPStartupPolicy `json:"policy" yaml:"policy"` // onStart or onDemand
Preinstalled bool `json:"preinstalled" yaml:"preinstalled"` // whether to install this MCP server when mcp-gateway starts
}
ArgConfig struct {
Name string `json:"name" yaml:"name"`
Position string `json:"position" yaml:"position"` // header, query, path, body
Required bool `json:"required" yaml:"required"`
Type string `json:"type" yaml:"type"`
Description string `json:"description" yaml:"description"`
Default string `json:"default" yaml:"default"`
Items ItemsConfig `json:"items,omitempty" yaml:"items,omitempty"`
}
ItemsConfig struct {
Type string `json:"type" yaml:"type"`
Enum []string `json:"enum,omitempty" yaml:"enum,omitempty"`
Properties map[string]any `json:"properties,omitempty" yaml:"properties,omitempty"`
Items *ItemsConfig `json:"items,omitempty" yaml:"items,omitempty"`
Required []string `json:"required,omitempty" yaml:"required,omitempty"`
}
// MCPConfigVersion represents a version of an MCP configuration
MCPConfigVersion struct {
Version int `json:"version" yaml:"version"`
CreatedBy string `json:"created_by" yaml:"created_by"`
CreatedAt time.Time `json:"created_at" yaml:"created_at"`
ActionType cnst.ActionType `json:"action_type" yaml:"action_type"` // Create, Update, Delete, Revert
Name string `json:"name" yaml:"name"`
Tenant string `json:"tenant" yaml:"tenant"`
Routers string `json:"routers" yaml:"routers"`
Servers string `json:"servers" yaml:"servers"`
Tools string `json:"tools" yaml:"tools"`
Prompts string `json:"prompts" yaml:"prompts"`
McpServers string `json:"mcp_servers" yaml:"mcp_servers"`
IsActive bool `json:"is_active" yaml:"is_active"` // indicates if this version is currently active
Hash string `json:"hash" yaml:"hash"` // hash of the configuration content
}
// Auth represents authentication configuration
Auth struct {
Mode cnst.AuthMode `json:"mode" yaml:"mode"`
}
PromptConfig struct {
Name string `json:"name" yaml:"name"`
Description string `json:"description" yaml:"description"`
Arguments []PromptArgument `json:"arguments" yaml:"arguments"`
PromptResponse []PromptResponse `json:"promptResponse,omitempty" yaml:"promptResponse,omitempty"`
}
PromptArgument struct {
Name string `json:"name" yaml:"name"`
Description string `json:"description" yaml:"description"`
Required bool `json:"required" yaml:"required"`
}
PromptResponse struct {
Role string `json:"role" yaml:"role"`
Content PromptResponseContent `json:"content" yaml:"content"`
}
PromptResponseContent struct {
Type string `json:"type" yaml:"type"`
Text string `json:"text" yaml:"text"`
}
)
// ToToolSchema converts a ToolConfig to a ToolSchema
func (t *ToolConfig) ToToolSchema() mcp.ToolSchema {
// Create properties map for input schema
properties := make(map[string]any)
required := make([]string, 0)
for _, arg := range t.Args {
property := map[string]any{
"type": arg.Type,
"description": arg.Description,
}
if arg.Type == "array" {
items := make(map[string]any)
if len(arg.Items.Enum) > 0 {
items["enum"] = lol.Union(arg.Items.Enum)
} else {
items["type"] = arg.Items.Type
// If items is an object type, recursively process its properties
if arg.Items.Properties != nil {
items["properties"] = arg.Items.Properties
}
}
property["items"] = items
}
properties[arg.Name] = property
if arg.Required {
required = append(required, arg.Name)
}
}
// Merge with existing input schema if any
if t.InputSchema != nil {
for k, v := range t.InputSchema {
properties[k] = v
}
}
result := mcp.ToolSchema{
Name: t.Name,
Title: t.Title,
Description: t.Description,
InputSchema: mcp.ToolInputSchema{
Type: "object",
Properties: properties,
Required: required,
},
}
// Add output schema if provided
if t.OutputSchema != nil {
result.OutputSchema = &mcp.ToolInputSchema{
Type: "object",
Properties: t.OutputSchema,
}
}
return result
}
// ToPromptSchema converts a PromptConfig to a PromptSchema
func (t *PromptConfig) ToPromptSchema() mcp.PromptSchema {
args := make([]mcp.PromptArgumentSchema, len(t.Arguments))
for i, a := range t.Arguments {
args[i] = mcp.PromptArgumentSchema{
Name: a.Name,
Description: a.Description,
Required: a.Required,
}
}
var responses []mcp.PromptResponseSchema
for _, r := range t.PromptResponse {
responses = append(responses, mcp.PromptResponseSchema{
Role: r.Role,
Content: mcp.PromptResponseContentSchema{
Type: r.Content.Type,
Text: r.Content.Text,
},
})
}
return mcp.PromptSchema{
Name: t.Name,
Description: t.Description,
Arguments: args,
PromptResponse: responses,
}
}