Skip to content

Commit f4f83d9

Browse files
committed
Merge remote-tracking branch 'upstream/main' into oauth-notification-refresh-flows
2 parents e7bb494 + 04c7f19 commit f4f83d9

File tree

4 files changed

+399
-309
lines changed

4 files changed

+399
-309
lines changed

pkg/gateway/capabilitites.go

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,42 @@ type ResourceTemplateRegistration struct {
4646
Handler mcp.ResourceHandler
4747
}
4848

49+
func (caps *Capabilities) getToolByName(toolName string) (ToolRegistration, error) {
50+
for _, tool := range caps.Tools {
51+
if tool.Tool.Name == toolName {
52+
return tool, nil
53+
}
54+
}
55+
return ToolRegistration{}, fmt.Errorf("unable to find tool")
56+
}
57+
58+
func (caps *Capabilities) getPromptByName(promptName string) (PromptRegistration, error) {
59+
for _, prompt := range caps.Prompts {
60+
if prompt.Prompt.Name == promptName {
61+
return prompt, nil
62+
}
63+
}
64+
return PromptRegistration{}, fmt.Errorf("unable to find prompt")
65+
}
66+
67+
func (caps *Capabilities) getResourceByURI(resourceURI string) (ResourceRegistration, error) {
68+
for _, resource := range caps.Resources {
69+
if resource.Resource.URI == resourceURI {
70+
return resource, nil
71+
}
72+
}
73+
return ResourceRegistration{}, fmt.Errorf("unable to find resource")
74+
}
75+
76+
func (caps *Capabilities) getResourceTemplateByURITemplate(resource string) (ResourceTemplateRegistration, error) {
77+
for _, template := range caps.ResourceTemplates {
78+
if template.ResourceTemplate.URITemplate == resource {
79+
return template, nil
80+
}
81+
}
82+
return ResourceTemplateRegistration{}, fmt.Errorf("unable to find resource template")
83+
}
84+
4985
func (g *Gateway) listCapabilities(ctx context.Context, configuration Configuration, serverNames []string, clientConfig *clientConfig) (*Capabilities, error) {
5086
var (
5187
lock sync.Mutex
@@ -222,17 +258,17 @@ func (g *Gateway) listCapabilities(ctx context.Context, configuration Configurat
222258
}, nil
223259
}
224260

225-
func (c *Capabilities) ToolNames() []string {
261+
func (caps *Capabilities) ToolNames() []string {
226262
var names []string
227-
for _, tool := range c.Tools {
263+
for _, tool := range caps.Tools {
228264
names = append(names, tool.Tool.Name)
229265
}
230266
return names
231267
}
232268

233-
func (c *Capabilities) PromptNames() []string {
269+
func (caps *Capabilities) PromptNames() []string {
234270
var names []string
235-
for _, prompt := range c.Prompts {
271+
for _, prompt := range caps.Prompts {
236272
names = append(names, prompt.Prompt.Name)
237273
}
238274
return names

pkg/gateway/dynamic_mcps.go

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ type ServerMatch struct {
209209
}
210210

211211
// mcpAddTool implements a tool for adding new servers to the registry
212-
func (g *Gateway) createMcpAddTool(configuration Configuration, clientConfig *clientConfig) *ToolRegistration {
212+
func (g *Gateway) createMcpAddTool(clientConfig *clientConfig) *ToolRegistration {
213213
tool := &mcp.Tool{
214214
Name: "mcp-add",
215215
Description: "Add a new MCP server to the session. The server must exist in the catalog.",
@@ -251,7 +251,7 @@ func (g *Gateway) createMcpAddTool(configuration Configuration, clientConfig *cl
251251
serverName := strings.TrimSpace(params.Name)
252252

253253
// Check if server exists in catalog
254-
serverConfig, _, found := configuration.Find(serverName)
254+
_, _, found := g.configuration.Find(serverName)
255255
if !found {
256256
return &mcp.CallToolResult{
257257
Content: []mcp.Content{&mcp.TextContent{
@@ -262,36 +262,34 @@ func (g *Gateway) createMcpAddTool(configuration Configuration, clientConfig *cl
262262

263263
// Append the new server to the current serverNames if not already present
264264
found = false
265-
for _, existing := range configuration.serverNames {
265+
for _, existing := range g.configuration.serverNames {
266266
if existing == serverName {
267267
found = true
268268
break
269269
}
270270
}
271271
if !found {
272-
configuration.serverNames = append(configuration.serverNames, serverName)
272+
g.configuration.serverNames = append(g.configuration.serverNames, serverName)
273273
}
274274

275275
// Fetch updated secrets for the new server list
276276
if g.configurator != nil {
277277
if fbc, ok := g.configurator.(*FileBasedConfiguration); ok {
278-
updatedSecrets, err := fbc.readDockerDesktopSecrets(ctx, configuration.servers, configuration.serverNames)
278+
updatedSecrets, err := fbc.readDockerDesktopSecrets(ctx, g.configuration.servers, g.configuration.serverNames)
279279
if err == nil {
280-
configuration.secrets = updatedSecrets
280+
g.configuration.secrets = updatedSecrets
281281
} else {
282282
log("Warning: Failed to update secrets:", err)
283283
}
284284
}
285285
}
286286

287-
// Update the current configuration state
288-
updatedServerNames := configuration.serverNames
289-
if err := g.reloadConfiguration(ctx, configuration, updatedServerNames, clientConfig); err != nil {
287+
if err := g.reloadServerConfiguration(ctx, serverName, clientConfig); err != nil {
290288
return nil, fmt.Errorf("failed to reload configuration: %w", err)
291289
}
292290

293291
// Register DCR client and start OAuth provider if this is a remote OAuth server
294-
if g.McpOAuthDcrEnabled && serverConfig.Spec.IsRemoteOAuthServer() {
292+
if g.McpOAuthDcrEnabled {
295293
// Register DCR client with DD so user can authorize
296294
if err := oauth.RegisterProviderForLazySetup(ctx, serverName); err != nil {
297295
logf("Warning: Failed to register OAuth provider for %s: %v", serverName, err)
@@ -315,7 +313,7 @@ func (g *Gateway) createMcpAddTool(configuration Configuration, clientConfig *cl
315313
}
316314

317315
// mcpRemoveTool implements a tool for removing servers from the registry
318-
func (g *Gateway) createMcpRemoveTool(_ Configuration, clientConfig *clientConfig) *ToolRegistration {
316+
func (g *Gateway) createMcpRemoveTool() *ToolRegistration {
319317
tool := &mcp.Tool{
320318
Name: "mcp-remove",
321319
Description: "Remove an MCP server from the registry and reload the configuration. This will disable the server.",
@@ -369,8 +367,8 @@ func (g *Gateway) createMcpRemoveTool(_ Configuration, clientConfig *clientConfi
369367
g.stopProvider(serverName)
370368
}
371369

372-
if err := g.reloadConfiguration(ctx, g.configuration, updatedServerNames, clientConfig); err != nil {
373-
return nil, fmt.Errorf("failed to reload configuration: %w", err)
370+
if err := g.removeServerConfiguration(ctx, serverName); err != nil {
371+
return nil, fmt.Errorf("failed to remove server configuration: %w", err)
374372
}
375373

376374
return &mcp.CallToolResult{
@@ -575,7 +573,7 @@ type configValue struct {
575573
}
576574

577575
// mcpConfigSetTool implements a tool for setting configuration values for MCP servers
578-
func (g *Gateway) createMcpConfigSetTool(configuration Configuration, clientConfig *clientConfig) *ToolRegistration {
576+
func (g *Gateway) createMcpConfigSetTool(clientConfig *clientConfig) *ToolRegistration {
579577
tool := &mcp.Tool{
580578
Name: "mcp-config-set",
581579
Description: "Set configuration values for MCP servers. Creates or updates server configuration with the specified key-value pairs.",
@@ -627,22 +625,22 @@ func (g *Gateway) createMcpConfigSetTool(configuration Configuration, clientConf
627625
configKey := strings.TrimSpace(params.Key)
628626

629627
// Check if server exists in catalog (optional check - we can configure servers that don't exist yet)
630-
_, _, serverExists := configuration.Find(serverName)
628+
_, _, serverExists := g.configuration.Find(serverName)
631629

632630
// Initialize the server's config map if it doesn't exist
633-
if configuration.config[serverName] == nil {
634-
configuration.config[serverName] = make(map[string]any)
631+
if g.configuration.config[serverName] == nil {
632+
g.configuration.config[serverName] = make(map[string]any)
635633
}
636634

637635
// Set the configuration value
638-
oldValue := configuration.config[serverName][configKey]
639-
configuration.config[serverName][configKey] = params.Value
636+
oldValue := g.configuration.config[serverName][configKey]
637+
g.configuration.config[serverName][configKey] = params.Value
640638

641639
// Log the configuration change
642640
log(fmt.Sprintf(" - Set config for server '%s': %s = %v", serverName, configKey, params.Value))
643641

644642
// Reload configuration with current server list to apply changes
645-
if err := g.reloadConfiguration(ctx, configuration, configuration.serverNames, clientConfig); err != nil {
643+
if err := g.reloadServerConfiguration(ctx, serverName, clientConfig); err != nil {
646644
return nil, fmt.Errorf("failed to reload configuration: %w", err)
647645
}
648646

0 commit comments

Comments
 (0)