MCP 动态注入
目前推荐一个服务是启动一个 agent,通过 userId 隔离。目前 skill 是已经支持,想了解下,MCP 是否支持?
可以使用工具分组 根据用户userId 所属的权限 进行激活
可以提供一个样例代码不?感谢。目前公司内部有自己 skill 广场和 mcp 广场,希望做到用户自己添加使用。目前我自己系统是每次添加技能和 mcp 的时候,重新建一个 agent,我看介绍文档是可以做到一个 agent,就可以动态自动注入 skill和 mcp 了。
在agent初始化的时候就将所有mcp工具注册到 Toolkit ,要分组,且为未激活状态
toolkit.createToolGroup(toolName, toolName, false, ToolGroupScope.EXTERNAL);
AgentTool tool = toolkit.getTool(toolName);
toolkit.registration()
.agentTool(tool)
.group(toolName)
.apply();
然后再使用的时候
List<String> activatedGroups = computeActivatedGroups(actAgent, ); // 根据当前用户请求添加的mcp进行计算激活哪些组哪些工具
AgentState state = actAgent.getAgentState(userId, conversationId);
state.getToolContext().setActivatedGroups(activatedGroups);
agentStateStore.save(userId, conversationId, "agent_state", state);
上面是一个参考方式,具体的要根据你的实际业务场景进行调整
谢谢大佬解答。但是这样会有几个问题。 1、对于10 个用户来说,就会需要有 10group。甚至更多。 2、对于用户频繁变更 MCP的话,这个已经注册的 toolkit 也不变。也需要重新构建agent 实例 不知道理解对不对,希望大佬可以指证下
谢谢大佬解答。但是这样会有几个问题。 1、对于10 个用户来说,就会需要有 10group。甚至更多。 2、对于用户频繁变更 MCP的话,这个已经注册的 toolkit 也不变。也需要重新构建agent 实例 不知道理解对不对,希望大佬可以指证下
1、在上述案例里面,如何10个用户的工具相同MCP TOOL 是同一个组, A 用户有 T1,T2.T3 B 用户有 T1,T4 那么底层默认支持总共 T1,T2.T3,T4 组 处于未激活状态,未激活的不会发送给大模型。 因为Agent是用户会话隔离的,每个人激活的工具,才会发送给大模型 2、你说的用户频繁变更 MCP,是指的多个MCP Server 还是 同一个MCP Server 下的 MCP tool 不过指的哪个都没影响,不需要重新构建agent 实例 重点在 Toolkit 的使用上,看 agentscope-spring-boot-starter 中的定义
这里一定要注意 使用,大白话就是不要破坏定义Toolkit Bean的原型特性。
{@code ObjectProvider<Toolkit>} or method injection.
不行,现在main分支的ReActAgent还不支持第二种方式,内部破坏了原型特性,还是需要重新构建agent 实例
谢谢大佬解析, 我理解了。一个工具为一组。按照用户绑定的工具找到所属组,setActivatedGroups。但是对于 MCP 而言,每个用户的mcp 配置不同,那注册成工具的话,是不是也不同工具呢?而且目前内部注解 mcp调用有报错。链接成功,不过有报错提示出来。
因为所有用户共享同一个 Toolkit,那么每个用户的mcp 配置不同,那注册成工具的话,也是不同工具。 这个可以类比权限系统: 注册的时候 1、设置为未激活状态 相当于进入了权限池子 2、记录当前用户与工具的绑定关系,相当于这个用户有这些权限
用户请求的时候 1、找到用户的权限(工具组)(当前用户与工具的绑定关系) 2、然后再激活对应的工具组
明白了。感觉大佬解答
补充一个和楼上「激活」相关但不同的角度:平台下发 / 集中托管 MCP 配置(MCP server 配置的来源,而不是激活)。
现状(核实 2.0.0 源码)—— skill 和 MCP 不对称:
- skill 有开箱的仓库抽象,平台下发是 turnkey:
agentscope-extensions-skill-git-repository/-mysql-repository/-postgresql-repository/agentscope-extensions-nacos-skill。docs 原话「MySQL/PostgreSQL/Nacos 动态修改、立即生效」——控制台改 skill → agent 下次getAllSkills()拿到,不用重启。 - MCP 没有对等的仓库抽象(没有
McpRepository/ git-mcp / nacos-mcp 扩展)。平台下发 MCP 配置目前要自己组装:平台维护 MCP server 配置(command/url + 凭据)→ agent 启动(或 Nacos listener 触发)时读配置,调:McpServerRegistrar.register(toolkit, Map<String,McpServerConfig>)(McpServerRegistrar.java:49,内部buildAsync().block(),阻塞注册)- 或逐个
toolkit.registerMcpClient(...)/removeMcpClient(...)(Toolkit.java:538/548,运行时可增删)
我按这个组装路径做了个 demo 实测(不是激活,是平台下发 MCP 配置):平台仓里放 mcp-servers.json,agent 启动读 + McpServerRegistrar.register + disableToolsConfig()(workspace tools.json 不自动加载)。实测:平台 commit 一个 MCP → agent 注册拿到其工具;平台清空 mcpServers → agent 无该 MCP(撤销)。仓库:https://github.com/helloworldtang/agentscope-java-e2e-pipeline(README 的「平台下发 MCP」一节)
提案:要不要加一个 McpRepository(或 McpConfigRepository)抽象对齐 AgentSkillRepository——后端 Git/MySQL/PG/Nacos,让平台托管 MCP 配置也变 turnkey(控制台改 MCP → agent 下次加载生效),跟 skill 一套体验。安全上 MCP 涉及凭证/网络,边界要比 skill 更严(只允许宿主代码读配置、不开放 agent 自助加 MCP——这点跟当前「无 McpManageTool」一致)。
这样「激活」(楼上 setActivatedGroups,运行时按用户开/关工具)+「下发」(平台仓库,MCP 配置从哪来)两端就齐了,多租户 / 平台化场景会更完整。求 maintainers 看法:是更倾向继续走「宿主自己组装 registerMcpClient」的路线,还是值得做一套 McpRepository 抽象?
+1 to the McpRepository / McpConfigRepository idea from the platform-delivery angle. Here is our scenario for context.
- Agents are embedded in many business services, and each is built once at startup.
- MCP servers are internal group systems, registered and approved centrally in a control plane (URL, service credential, timeout).
- We want the business services to take MCP server configuration from that control plane with no code changes, the same way skills already work through
AgentSkillRepository(a console change takes effect on the next call).
We already keep the agent side free of per-tool code: connection config only (McpServerConfig), with permissions driven by the server's readOnlyHint and the default ASK. So the missing piece is only where the config comes from, plus picking up changes without rebuilding the agent. Today that needs an agent rebuild or an application restart, because Toolkit.registerMcpClient / removeMcpClient at runtime are not guaranteed to reach the toolkit copy an agent actually uses.
A repository abstraction with refresh semantics aligned with skills would cover this. We agree the config should stay host-only, with no agent self-service tool for adding MCP servers.
Issue Evaluation
Category: question | Status: Answered (discussion evolving into an enhancement)
Verified against current main:
- Per-user session isolation is available today.
Toolkit.registerMcpClient(...)(agentscope-core/.../tool/Toolkit.java:653) plus tool groups let you register MCP tools into groups that are inactive by default (Toolkit.createToolGroup(name, desc, active)), and activation state is stored per session inToolContextState(getActivatedGroups/ mutation APIs,ToolContextState.java:112-186).ReActAgentfilters the request's tool schemas by the session's activated groups (ReActAgent.java:2452-2455), so a shared agent instance can expose different MCP tool sets persessionId/userId— the lazy-activation model proposed earlier in this thread is supported by the current design. - What is missing: a config source abstraction. Skills have repository extensions (e.g. the git/mysql skill repositories), but MCP servers are only wired at build time (e.g.
McpServerRegistrarin agentscope-harness consumes a staticMap<String, McpServerConfig>). There is noMcpRepository-style, platform-deliverable MCP config store in core or extensions today.
So: dynamic per-user activation — yes, via tool groups + per-session ToolContextState; centralized/dynamic MCP config delivery (the +1'd McpRepository idea) — not implemented yet. Given the traction in this thread, suggesting we track that part as an enhancement.
Automated evaluation by github-manager-bot