Skip to content

Commit 7f6d46c

Browse files
committed
feature(llm): add mcp-agent
1 parent 518f81b commit 7f6d46c

File tree

16 files changed

+1761
-6
lines changed

16 files changed

+1761
-6
lines changed

cmd/climc/shell/llm/mcp_agent.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package llm
2+
3+
import (
4+
"yunion.io/x/onecloud/cmd/climc/shell"
5+
modules "yunion.io/x/onecloud/pkg/mcclient/modules/llm"
6+
base_options "yunion.io/x/onecloud/pkg/mcclient/options"
7+
options "yunion.io/x/onecloud/pkg/mcclient/options/llm"
8+
)
9+
10+
func init() {
11+
cmd := shell.NewResourceCmd(&modules.MCPAgent)
12+
cmd.List(new(options.MCPAgentListOptions))
13+
cmd.Show(new(options.MCPAgentShowOptions))
14+
cmd.Create(new(options.MCPAgentCreateOptions))
15+
cmd.Update(new(options.MCPAgentUpdateOptions))
16+
cmd.Delete(new(options.MCPAgentDeleteOptions))
17+
cmd.Perform("public", &base_options.BasePublicOptions{})
18+
cmd.Perform("private", &base_options.BaseIdOptions{})
19+
cmd.Get("mcp-tools", new(options.MCPAgentIdOptions))
20+
cmd.Get("tool-request", new(options.MCPAgentToolRequestOptions))
21+
cmd.Get("chat-test", new(options.MCPAgentChatTestOptions))
22+
cmd.Get("request", new(options.MCPAgentMCPAgentRequestOptions))
23+
}

pkg/apis/llm/mcp_agent.go

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package llm
2+
3+
import (
4+
"yunion.io/x/pkg/util/sets"
5+
6+
"yunion.io/x/onecloud/pkg/apis"
7+
)
8+
9+
// LLMClientType 定义 LLM 驱动类型
10+
type LLMClientType string
11+
12+
const (
13+
LLM_CLIENT_OLLAMA LLMClientType = "ollama"
14+
LLM_CLIENT_OPENAI LLMClientType = "openai"
15+
16+
MCP_AGENT_SYSTEM_PROMPT = `你是一个 Cloudpods 云平台管理助手。你可以使用提供的工具来帮助用户管理云资源。
17+
18+
## 你的能力
19+
- 查询云平台资源(虚拟机、镜像、网络、存储、区域等)
20+
- 管理虚拟机(创建、启动、停止、重启、删除、重置密码)
21+
- 获取虚拟机监控信息和实时统计数据
22+
23+
## 工作流程
24+
1. 理解用户的需求
25+
2. 选择合适的工具来完成任务
26+
3. 分析工具返回的结果
27+
4. 如果需要更多信息,继续调用其他工具
28+
5. 最后用自然语言总结结果给用户
29+
30+
## 注意事项
31+
- 认证信息已由系统自动处理,调用工具时无需提供认证参数
32+
- 如果工具调用失败,尝试分析错误原因并告知用户
33+
- 回复时使用中文,语言简洁明了
34+
`
35+
)
36+
37+
var (
38+
LLM_CLIENT_TYPES = sets.NewString(
39+
string(LLM_CLIENT_OLLAMA),
40+
string(LLM_CLIENT_OPENAI),
41+
)
42+
)
43+
44+
// IsLLMClientType 检查给定的字符串是否是有效的 LLM 驱动类型
45+
func IsLLMClientType(t string) bool {
46+
return LLM_CLIENT_TYPES.Has(t)
47+
}
48+
49+
// MCP Agent 配置相关的 API 定义
50+
type MCPAgentListInput struct {
51+
apis.SharableVirtualResourceListInput
52+
53+
LLMDriver string `json:"llm_driver"`
54+
}
55+
56+
type MCPAgentCreateInput struct {
57+
apis.SharableVirtualResourceCreateInput
58+
59+
LLMId string `json:"llm_id" help:"LLM 实例 ID,如果提供则自动获取 llm_url"`
60+
LLMUrl string `json:"llm_url" help:"后端大模型的 base 请求地址"`
61+
LLMDriver string `json:"llm_driver" help:"使用的大模型驱动,可以是 ollama 或 openai"`
62+
Model string `json:"model" help:"使用的模型名称"`
63+
ApiKey string `json:"api_key" help:"在 llm_driver 中需要用到的认证"`
64+
McpServer string `json:"mcp_server" help:"mcp 服务器的后端地址"`
65+
}
66+
67+
type MCPAgentUpdateInput struct {
68+
apis.SharableVirtualResourceCreateInput
69+
70+
LLMId *string `json:"llm_id,omitempty" help:"LLM 实例 ID,如果提供则自动获取 llm_url"`
71+
LLMUrl *string `json:"llm_url,omitempty" help:"后端大模型的 base 请求地址"`
72+
LLMDriver *string `json:"llm_driver,omitempty" help:"使用的大模型驱动,可以是 ollama 或 openai"`
73+
Model *string `json:"model,omitempty" help:"使用的模型名称"`
74+
ApiKey *string `json:"api_key,omitempty" help:"在 llm_driver 中需要用到的认证"`
75+
McpServer *string `json:"mcp_server,omitempty" help:"mcp 服务器的后端地址"`
76+
}
77+
78+
type MCPAgentDetails struct {
79+
apis.SharableVirtualResourceDetails
80+
81+
LLMUrl string `json:"llm_url"`
82+
LLMDriver string `json:"llm_driver"`
83+
Model string `json:"model"`
84+
ApiKey string `json:"api_key"`
85+
McpServer string `json:"mcp_server"`
86+
}
87+
88+
type LLMToolRequestInput struct {
89+
ToolName string `json:"tool_name"`
90+
Arguments map[string]interface{} `json:"arguments"`
91+
}
92+
93+
type LLMChatTestInput struct {
94+
Message string `json:"message" help:"test message to send to LLM"`
95+
}
96+
97+
type LLMMCPAgentRequestInput struct {
98+
Query string `json:"query" help:"query to send to MCP agent"`
99+
}
100+
101+
// MCPAgentResponse 表示 Agent 响应
102+
type MCPAgentResponse struct {
103+
// Success 是否成功
104+
Success bool `json:"success"`
105+
// Answer 自然语言回答
106+
Answer string `json:"answer"`
107+
// Error 错误信息
108+
Error string `json:"error,omitempty"`
109+
// ToolCalls 工具调用记录
110+
ToolCalls []MCPAgentToolCallRecord `json:"tool_calls,omitempty"`
111+
}
112+
113+
// MCPAgentToolCallRecord 记录工具调用
114+
type MCPAgentToolCallRecord struct {
115+
ToolName string `json:"tool_name"`
116+
Arguments map[string]interface{} `json:"arguments"`
117+
Result string `json:"result"`
118+
}
119+
120+
const (
121+
// MCPAgentMaxIterations 最大迭代次数,防止无限循环
122+
MCPAgentMaxIterations = 10
123+
)

pkg/apis/llm/ollama_const.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ const (
88
LLM_OLLAMA_CREATE_ACTION = "create"
99
LLM_OLLAMA_EXPORT_ENV_KEY = "OLLAMA_HOST"
1010
LLM_OLLAMA_EXPORT_ENV_VALUE = "0.0.0.0:11434"
11+
LLM_OLLAMA_DEFAULT_PORT = 11434
1112
)
1213

1314
const (

pkg/llm/drivers/llm_client/doc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
package llm_client

0 commit comments

Comments
 (0)