Repository navigation
Feature/mcp capabilities sync - #245
Merged
Merged
Conversation
…盖旧数据(stale-while-revalidate)\n- 新增能力查看页面与前端接口集成\n- 扩展MCP能力结构体定义与服务端聚合逻辑\n- 增强配置管理接口(列表/版本/激活)\n\nBREAKING CHANGE: 取消了通过访问页面才触发能力拉取的设计,改为后端后台任务定时刷新。 Signed-off-by: x22x22 <[email protected]>
Contributor
Reviewer's GuideThis PR adds end-to-end support for automatic discovery, caching, and management of MCP service capabilities by introducing a background refresher and cache with TTL, a new GET endpoint to retrieve capabilities, enhanced data models, and a full-featured frontend interface for viewing, searching, and exporting capabilities. Sequence diagram for MCP capabilities discovery and cachingsequenceDiagram
participant GatewayUser as actor Gateway User
participant Frontend as Frontend (Gateway Page)
participant Backend as Backend (API Server)
participant MCP as MCP Handler
participant MCPServer as MCP Service
Note over Frontend,Backend: User requests capabilities for a server
GatewayUser->>Frontend: Clicks 'View Capabilities' button
Frontend->>Backend: GET /api/mcp/capabilities/:tenant/:name
Backend->>MCP: HandleGetCapabilities(tenant, name)
alt Cache hit and not expired
MCP->>Backend: Return cached capabilities
else Cache miss or expired
MCP->>MCPServer: Fetch capabilities (tools, prompts, resources, templates)
MCPServer-->>MCP: Capabilities data
MCP->>MCP: Update cache
MCP->>Backend: Return fresh capabilities
end
Backend->>Frontend: Capabilities data
Frontend->>GatewayUser: Display capabilities viewer
ER diagram for new MCP capabilities data typeserDiagram
MCPCapabilities {
Tool[] tools
Prompt[] prompts
Resource[] resources
ResourceTemplate[] resourceTemplates
}
Tool {
string name
string description
ToolInputSchema inputSchema
ToolAnnotations annotations
bool enabled
time lastSynced
}
Prompt {
string name
string description
PromptArgumentSchema[] arguments
time lastSynced
}
Resource {
string uri
string name
string description
string mimeType
time lastSynced
}
ResourceTemplate {
string uriTemplate
string name
string description
string mimeType
time lastSynced
}
MCPCapabilities ||--|{ Tool: "tools"
MCPCapabilities ||--|{ Prompt: "prompts"
MCPCapabilities ||--|{ Resource: "resources"
MCPCapabilities ||--|{ ResourceTemplate: "resourceTemplates"
Class diagram for MCP capabilities cache and handlerclassDiagram
class MCP {
+database.Database db
+storage.Store store
+notifier.Notifier notifier
+zap.Logger logger
+sync.Map capabilitiesCache
+time.Duration refreshInterval
+StartCapabilitiesSync(ctx)
+HandleGetCapabilities(c)
+refreshAllCapabilities(ctx)
+refreshCapabilitiesForConfig(ctx, cfg)
+updateCapabilitiesCache(cacheKey, data)
+getCapabilitiesFromCache(ctx, cacheKey, cfg)
+clearCapabilitiesCache(key)
}
class cachedCapabilities {
+mcp.CapabilitiesInfo data
+time.Time timestamp
+time.Duration ttl
+isExpired()
}
MCP "1" o-- "*" cachedCapabilities
class CapabilitiesInfo {
+Tool[] Tools
+Prompt[] Prompts
+Resource[] Resources
+ResourceTemplate[] ResourceTemplates
+time.Time LastSynced
+ImplementationSchema ServerInfo
}
class Tool {
+string Name
+string Description
+ToolInputSchema InputSchema
+ToolAnnotations Annotations
+bool Enabled
+time.Time LastSynced
}
class Prompt {
+string Name
+string Description
+PromptArgumentSchema[] Arguments
+time.Time LastSynced
}
class Resource {
+string Uri
+string Name
+string Description
+string MimeType
+time.Time LastSynced
}
class ResourceTemplate {
+string UriTemplate
+string Name
+string Description
+string MimeType
+time.Time LastSynced
}
CapabilitiesInfo "1" o-- "*" Tool
CapabilitiesInfo "1" o-- "*" Prompt
CapabilitiesInfo "1" o-- "*" Resource
CapabilitiesInfo "1" o-- "*" ResourceTemplate
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Contributor
There was a problem hiding this comment.
Hey there - I've reviewed your changes - here's some feedback:
- Extract the cache-key construction (tenant + ':' + name) into a single helper method to eliminate repeated string concatenations and potential typos.
- Avoid using context.WithoutCancel for your background refresh goroutines so that they properly stop when the parent context is canceled instead of leaking.
- Make the hardcoded refreshInterval (120s) and cache TTL (5m) configurable via flags or config rather than fixed constants to give operators more control.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Extract the cache-key construction (tenant + ':' + name) into a single helper method to eliminate repeated string concatenations and potential typos.
- Avoid using context.WithoutCancel for your background refresh goroutines so that they properly stop when the parent context is canceled instead of leaking.
- Make the hardcoded refreshInterval (120s) and cache TTL (5m) configurable via flags or config rather than fixed constants to give operators more control.
## Individual Comments
### Comment 1
<location> `internal/apiserver/handler/mcp.go:124` </location>
<code_context>
+// It will not overwrite existing cache with empty/failed data.
+func (h *MCP) refreshCapabilitiesForConfig(ctx context.Context, cfg *config.MCPConfig) {
+ cacheKey := cfg.Tenant + ":" + cfg.Name
+ capabilities, err := h.fetchCapabilities(ctx, cfg)
+ if err != nil {
+ // Do not remove previous cache on error
</code_context>
<issue_to_address>
fetchCapabilities does not implement resource/resourceTemplate fetching.
Add a clear error or warning for unimplemented resource/resourceTemplate fetching to prevent silent failures, or document this limitation for users.
</issue_to_address>
### Comment 2
<location> `web/src/pages/gateway/components/CapabilitiesViewer.tsx:756` </location>
<code_context>
+ selectedType: CapabilityType | 'all',
+ advancedFilters: typeof advancedSearch
+ ): MCPCapabilities => {
+ const filtered: MCPCapabilities = {};
+
+ const matchesSearch = (item: {name: string, description?: string}) => {
</code_context>
<issue_to_address>
filterCapabilities may return undefined arrays.
Initialize all capability type arrays to empty to prevent undefined values and simplify downstream usage.
Suggested implementation:
```typescript
// Initialize all capability type arrays to empty arrays
const filtered: MCPCapabilities = {
source: [],
sink: [],
processor: [],
// Add other capability types here if they exist in MCPCapabilities
};
```
If MCPCapabilities includes more capability types than 'source', 'sink', and 'processor', you should add those as well to the initialization block.
</issue_to_address>Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
… TTL; extract cache key helper; remove context.WithoutCancel; warn on unimplemented resources
…TTL config + CLI overrides
…litiesViewer component
…nted resource fetching
| h.logger.Warn("tenant parameter required but missing") | ||
| i18n.RespondWithError(c, i18n.ErrorTenantRequired) | ||
| return | ||
| } |
Check failure
Code scanning / CodeQL
Size computation for allocation may overflow
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Add MCP capability button on Gateway page, supporting automatic discovery of MCP service capabilities:
Main functional modules:
Interface effects:
Summary by Sourcery
Add full-stack support for MCP capabilities synchronization and management, including a new API, caching and background refresh on the backend, and an interactive viewer UI in the gateway frontend that allows browsing, filtering, exporting, and toggling MCP service capabilities.
New Features:
Enhancements: