
使用 MCP Toolbox 的 mongodb-insert-many 工具批量写入 MongoDB 文档【免费下载链接】mcp-toolboxMCP Toolbox for Databases is an open source MCP server for databases.项目地址: https://gitcode.com/GitHub_Trending/ge/mcp-toolbox本指南深入讲解 MCP Toolbox for Databases 中mongodb-insert-many工具的使用方法它允许 LLM Agent 通过一次调用向 MongoDB 集合批量插入多条文档非常适合日志批量入库、事件采集、数据导入等场景。读完本文你将掌握该工具的完整配置字段、data参数格式、canonical解析模式的选择依据以及集合的静态/动态绑定策略并理解其背后的源码实现原理。工具概述mongodb-insert-many是 MCP Toolbox 中 MongoDB 系列工具之一用于在单次批量操作中向指定的 MongoDB 集合插入多条新文档。相比逐条插入批量插入对于一次性写入大量数据更为高效。该工具只要求一个运行时参数data它必须是一个JSON 数组字符串数组元素为文档对象。插入成功后工具会返回一个 JSON 数组其中包含每一条新建文档的唯一_id方便上层 Agent 核对写入结果。从源码结构看该工具在 internal/tools/mongodb/mongodbinsertmany/mongodbinsertmany.go 中通过tools.Register(resourceType, newConfig)注册资源类型常量为mongodb-insert-many与配置中的type字段一一对应。适用来源Compatible Sourcesmongodb-insert-many仅适用于mongodb类型的来源Source。在源码层面工具通过compatibleSource接口约束来源必须实现MongoClient()与InsertMany(...)方法见 mongodbinsertmany.go不满足该接口的来源在ValidateSource阶段会直接报错。mongodb来源的最小配置示例如下详见 MongoDB 来源文档kind: source name: my-mongodb type: mongodb uri: mongodbsrv://username:passwordhost.mongodb.net其中uri为 MongoDB Atlas或自建实例连接串type必须为mongodb。来源初始化时会执行client.Ping校验连接可用性并设置应用名为工具箱生成的 UserAgent见 internal/sources/mongodb/mongodb.go。工具配置示例以下是官方文档给出的完整示例定义了一个名为log_batch_events的工具用于一次性写入多条事件日志kind: tool name: log_batch_events type: mongodb-insert-many source: my-mongo-source description: Inserts a batch of event logs into the database. database: logging collection: events canonical: trueLLM 调用该工具时只需把文档数组以 JSON 字符串形式传给data参数tool_code: log_batch_events(data[{event: login, user: user1}, {event: click, user: user2}, {event: logout, user: user1}])上例中canonical: true意味着data字符串将按 MongoDBCanonical Extended JSON格式解析如果数据中不存在特殊类型如$oid、$date保持默认canonical: falseRelaxed 模式即可。字段参考下表完整列出该工具的所有配置字段fieldtyperequireddescriptiontypestringtrueMust bemongodb-insert-many.sourcestringtrueThe name of themongodbsource to use.descriptionstringtrueA description of the tool that is passed to the LLM.databasestringtrueThe name of the MongoDB database containing the collection.collectionstringfalseThe name of the MongoDB collection into which the documents will be inserted. Mutually exclusive withcollectionAllowedValues. If omitted, it must be supplied at runtime as acollectionparameter, and can be restricted withcollectionAllowedValues.collectionAllowedValueslistfalseAn optional list of collection names the agent may choose from whencollectionis provided at runtime. Only configure this if collection is omitted.canonicalboolfalseDetermines if the data string is parsed using MongoDBs Canonical or Relaxed Extended JSON format. Defaults tofalse.几点实战注意事项description非空是硬性要求配置校验阶段若为空会返回description is required for tool ...错误见 mongodbinsertmany.go。collection与collectionAllowedValues互斥同时配置会报错见 mongodbcommon/util.go 中ValidateCollectionConfig的实现。当collection在配置中省略时工具会自动暴露一个必填的运行时collection字符串参数若同时配置了collectionAllowedValues该参数还会被限制为仅允许列表内的集合名见 util.go。这一机制在 mongodbinsertmany_test.go 的TestRuntimeCollection中有完整覆盖。该工具默认标注为破坏性destructive操作readOnlyHint为 false提示调用方写入会修改数据。调用行为与返回结果工具的执行链路分为两层工具层mongodbinsertmany.go校验data参数存在且为字符串解析集合来源配置中的collection优先否则取运行时参数然后调用来源的InsertMany(ctx, jsonData, t.Cfg.Canonical, t.Cfg.Database, collection)。来源层internal/sources/mongodb/mongodb.go使用bson.UnmarshalExtJSON([]byte(jsonData), canonical, data)将 JSON 数组解析为 BSON 文档数组再调用mongo-driver的Collection.InsertMany执行批量写入最后把res.InsertedIDs即全部新文档的_id列表返回给调用方。因此调用方拿到的是形如[65f1a2b3c4d5e6f7a8b9c0d1, ...]的_id数组可用于后续的单条查询或更新操作。若 JSON 解析失败或写入失败工具会返回 Agent 错误并附带可读的错误信息通过util.ProcessGeneralError统一包装。常见使用场景与注意事项典型场景事件日志批量入库、用户行为数据回填、测试数据灌入。一次data调用即可替代多条mongodb-insert-one减少 Agent 与数据库之间的往返次数。使用建议批量数据量较大时注意控制单次data的体积避免超出 MCP 消息大小限制需要写入带ObjectId、日期等特殊类型字段的文档时必须按 Canonical Extended JSON 格式书写并设置canonical: true例如{_id: {$oid: ...}, ts: {$date: ...}}纯 JSON 文档则保持canonical: false默认即可如果希望复用同一工具向多个集合写入不要配置collection改为让 Agent 每次调用时通过运行时collection参数指定并可用collectionAllowedValues白名单约束可选范围该工具执行的是写操作请结合 MCP Toolbox 的认证auth机制控制谁可以调用避免未授权写入。关于canonical与collection配置解析的正确性可参考 mongodbinsertmany_test.go 中的TestParseFromYamlMongoQuery它覆盖了默认、canonical: true、canonical: false三种解析结果以及缺少database时配置解析失败的负例可作为排查配置问题的对照依据。【免费下载链接】mcp-toolboxMCP Toolbox for Databases is an open source MCP server for databases.项目地址: https://gitcode.com/GitHub_Trending/ge/mcp-toolbox创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考