载入中...
搜索中...
未找到
McpToolFamilies.cpp
浏览该文件的文档.
2
5
6#include <Poco/JSON/Parser.h>
7#include <set>
8#include <stdexcept>
9
10namespace eve::dev {
11McpToolFamilyCatalog::McpToolFamilyCatalog(const std::vector<McpToolFamily>& families) {
12 std::set<std::string_view> ids;
13 // All state belongs to this unexposed object until construction succeeds.
14 for (const auto& family : families) {
15 if (family.id.empty() || !family.schemas || !family.route.call || !ids.insert(family.id).second) {
16 throw std::invalid_argument("invalid or duplicate MCP family: " + std::string(family.id));
17 }
18 Poco::JSON::Array::Ptr tools;
19 try {
20 Poco::JSON::Parser parser;
21 tools = parser.parse("[" + std::string(family.schemas()) + "]").extract<Poco::JSON::Array::Ptr>();
22 } catch (const std::exception& error) {
23 throw std::invalid_argument("invalid MCP schemas in " + std::string(family.id) + ": " + error.what());
24 }
25 for (std::size_t i = 0; i < tools->size(); ++i) {
26 const auto tool = tools->getObject(static_cast<unsigned>(i));
27 if (!tool || !tool->has("name") || !tool->get("name").isString() || !tool->getObject("inputSchema")) {
28 throw std::invalid_argument("MCP schema needs a string name and object inputSchema: " +
29 std::string(family.id));
30 }
31 const auto name = tool->getValue<std::string>("name");
32 if (name.empty() || !routes_.emplace(name, family.route).second) {
33 throw std::invalid_argument("empty or duplicate MCP tool name: " + name);
34 }
35 if (!schemas_.empty()) schemas_ += ',';
36 schemas_ += mcpStringify(Poco::Dynamic::Var(tool));
37 }
38 }
39}
40
41std::optional<McpToolRoute> McpToolFamilyCatalog::find(std::string_view name) const {
42 const auto found = routes_.find(name);
43 if (found == routes_.end()) return std::nullopt;
44 return found->second;
45}
46
48 // The detached stdio reader can outlive normal static teardown, exactly
49 // like McpServer. Keep its immutable routing snapshot alive with that host.
50 struct CompiledCatalog {
52 };
53 return Immortal<CompiledCatalog>::get().value;
54}
55} // namespace eve::dev
std::string name
Definition ECS.cpp:154
std::string error
Definition Package.cpp:174
Object::Ptr value
bool found
Immutable schema/route catalogue built transactionally from compiled families. @ownership Owns schema...
std::optional< McpToolRoute > find(std::string_view name) const
Return an owned route copy, or no route for an unknown name; safe for concurrent const reads.
McpToolFamilyCatalog(const std::vector< McpToolFamily > &families)
Build and validate one complete snapshot, publishing nothing on failure.
std::unordered_map< const void *, size_t > ids
Definition Snapshot.cpp:109
std::string mcpStringify(const Poco::Dynamic::Var &value)
Compact single-line JSON text for a newline-framed MCP payload.
Definition McpJson.hpp:24
std::vector< McpToolFamily > compiledMcpToolFamilies()
Generated build input; returns binary-owned descriptors for exactly the compiled providers....
const McpToolFamilyCatalog & compiledMcpToolCatalog()
Build the process-immortal compiled catalogue once, then borrow it until exit. @lifetime Survives sta...
static T & get()
Returns the process-lifetime instance.
Definition Immortal.hpp:21