7#include <unordered_set>
13std::string g_processProfile;
14std::string g_sessionProfile;
15McpFilterStats g_stats;
16bool g_processLoaded =
false;
18void ensureProcessLoadedUnlocked() {
19 if (g_processLoaded)
return;
20 if (
const char*
raw = std::getenv(
"EVE_MCP_PROFILE")) {
21 g_processProfile =
raw;
23 g_processLoaded =
true;
26bool isKnownTaskKind(std::string_view
kind) {
27 return kind ==
"content" ||
kind ==
"script" ||
kind ==
"verify" ||
kind ==
"fix" ||
kind ==
"workshop";
30bool isKnownProfile(std::string_view profile) {
31 return profile ==
"content.scene" || profile ==
"content.map" || profile ==
"script.bind" ||
32 profile ==
"verify.play" || profile ==
"fix.compile" || profile ==
"workshop.rpg";
35std::string defaultProfileForKind(std::string_view
kind) {
36 if (
kind ==
"content")
return "content.scene";
37 if (
kind ==
"script")
return "script.bind";
38 if (
kind ==
"fix")
return "fix.compile";
39 if (
kind ==
"workshop")
return "workshop.rpg";
43std::string taskKindForProfile(std::string_view profile) {
44 if (profile.starts_with(
"content."))
return "content";
45 if (profile.starts_with(
"script."))
return "script";
46 if (profile.starts_with(
"fix."))
return "fix";
47 if (profile.starts_with(
"workshop."))
return "workshop";
51bool startsWith(std::string_view
value, std::string_view prefix) {
52 return value.size() >= prefix.size() &&
value.compare(0, prefix.size(), prefix) == 0;
55bool equalsAny(std::string_view
value, std::initializer_list<const char*> names) {
56 for (
const char*
name : names) {
62bool isDangerEvalTool(std::string_view tool) {
return tool ==
"eve_eval" || tool ==
"eve_run_script"; }
64bool isAgentSessionTool(std::string_view tool) {
return startsWith(tool,
"eve_agent_"); }
66bool isCoreDiscoverTool(std::string_view tool) {
67 return equalsAny(tool, {
"eve_status",
"eve_game_world",
"eve_ai_log",
"eve_ai_note",
"eve_crash_report",
68 "eve_console_read",
"eve_console_write",
"eve_console_clear"}) ||
69 startsWith(tool,
"eve_asset_kit_");
72bool tagsAllow(std::string_view tool,
const std::unordered_set<std::string>& tags) {
74 if (
tags.count(
tag) != 0)
return true;
82 std::string* outTaskKind, std::string* errorCode, std::string* errorMessage) {
83 std::string
kind = std::string(taskKind);
84 std::string prof = std::string(profile);
85 if (
kind.empty() && prof.empty()) {
88 }
else if (
kind.empty()) {
89 if (!isKnownProfile(prof)) {
90 if (errorCode) *errorCode =
"invalid-profile";
91 if (errorMessage) *errorMessage =
"profile must be a known task profile id";
94 kind = taskKindForProfile(prof);
95 }
else if (prof.empty()) {
96 if (!isKnownTaskKind(
kind)) {
97 if (errorCode) *errorCode =
"invalid-task-kind";
98 if (errorMessage) *errorMessage =
"taskKind must be content, script, verify, fix, or workshop";
101 prof = defaultProfileForKind(
kind);
103 if (!isKnownTaskKind(
kind)) {
104 if (errorCode) *errorCode =
"invalid-task-kind";
105 if (errorMessage) *errorMessage =
"taskKind must be content, script, verify, fix, or workshop";
108 if (!isKnownProfile(prof)) {
109 if (errorCode) *errorCode =
"invalid-profile";
110 if (errorMessage) *errorMessage =
"profile must be a known task profile id";
113 if (taskKindForProfile(prof) !=
kind) {
114 if (errorCode) *errorCode =
"profile-task-mismatch";
115 if (errorMessage) *errorMessage =
"profile does not belong to the given taskKind";
119 if (outProfile) *outProfile = std::move(prof);
120 if (outTaskKind) *outTaskKind = std::move(
kind);
125 if (profile ==
"content.scene")
return {
"agent",
"core",
"scene"};
126 if (profile ==
"content.map")
return {
"agent",
"core",
"editor"};
127 if (profile ==
"script.bind")
return {
"agent",
"core",
"debug"};
128 if (profile ==
"verify.play")
return {
"agent",
"core",
"play",
"input"};
129 if (profile ==
"fix.compile")
return {
"agent",
"core",
"debug"};
130 if (profile ==
"workshop.rpg")
return {
"agent",
"core",
"play",
"input",
"editor",
"scene"};
138 if (!
out.empty())
out +=
',';
145 if (profile.empty())
return true;
146 if (!isKnownProfile(profile))
return false;
149 if (isAgentSessionTool(tool) || tool ==
"eve_status" || tool ==
"eve_game_world" ||
150 startsWith(tool,
"eve_asset_kit_") || tool ==
"eve_scene_search" || startsWith(tool,
"eve_scene_search_"))
153 if (isDangerEvalTool(tool)) {
155 return profile ==
"fix.compile";
158 if (tool ==
"eve_workshop")
return profile ==
"workshop.rpg";
161 if (tool ==
"eve_recipe_apply")
return profile ==
"content.scene" || profile ==
"workshop.rpg";
164 std::unordered_set<std::string>
tags(tagList.begin(), tagList.end());
166 if (profile ==
"script.bind") {
167 if (equalsAny(tool, {
"eve_api_search",
"eve_api_get",
"eve_console_read",
"eve_console_write",
168 "eve_console_clear",
"eve_error_slice",
"eve_crash_report"}))
171 if (equalsAny(tool, {
"eve_stack",
"eve_locals",
"eve_list_breakpoints"}))
return true;
175 if (profile ==
"verify.play") {
176 if (equalsAny(tool, {
"eve_screenshot",
"eve_screenshot_image",
"eve_render_status",
"eve_console_read"}))
178 return tagsAllow(tool,
tags);
181 if (profile ==
"fix.compile") {
182 if (isCoreDiscoverTool(tool) || tool ==
"eve_restart" || tool ==
"eve_error_slice" ||
183 tool ==
"eve_api_search" || tool ==
"eve_api_get")
185 return tagsAllow(tool,
tags);
188 if (profile ==
"content.scene") {
189 if (startsWith(tool,
"eve_procgen_") || startsWith(tool,
"eve_recipe_") ||
190 startsWith(tool,
"eve_asset_kit_") || tool ==
"eve_scene_info" || tool ==
"eve_scene_director_status")
192 return tagsAllow(tool,
tags);
195 if (profile ==
"content.map") {
196 if (tool ==
"eve_console_read")
return true;
197 return tagsAllow(tool,
tags);
200 if (profile ==
"workshop.rpg") {
201 if (tool ==
"eve_workshop" || tool ==
"eve_screenshot" || tool ==
"eve_screenshot_image" ||
202 tool ==
"eve_console_read" || tool ==
"eve_api_search" || tool ==
"eve_api_get")
204 return tagsAllow(tool,
tags);
207 return tagsAllow(tool,
tags);
216 if (isDangerEvalTool(tool) && profile !=
"fix.compile") {
217 hint.
to =
"fix.compile";
218 hint.
reason =
"eval and run_script are limited to fix.compile and cannot satisfy required criteria";
221 if ((startsWith(tool,
"eve_scene_") || startsWith(tool,
"eve_camera_") || startsWith(tool,
"eve_procgen_") ||
222 startsWith(tool,
"eve_recipe_") || tool ==
"eve_whitebox_playtest") &&
223 (profile ==
"verify.play" || profile ==
"script.bind" || profile ==
"fix.compile")) {
224 hint.
to =
"content.scene";
225 hint.
reason =
"scene, recipe, or procgen mutation requires content.scene";
228 if (tool ==
"eve_workshop" && profile !=
"workshop.rpg") {
229 hint.
to =
"workshop.rpg";
230 hint.
reason =
"workshop facade is limited to workshop.rpg";
233 if ((startsWith(tool,
"eve_editor_") || startsWith(tool,
"eve_host_")) &&
234 (profile ==
"verify.play" || profile ==
"script.bind" || profile ==
"content.scene")) {
235 hint.
to = profile ==
"content.scene" ?
"content.map" :
"workshop.rpg";
236 hint.
reason =
"editor/host tools require content.map or workshop.rpg";
239 if ((tool ==
"eve_play" || tool ==
"eve_gameplay" || startsWith(tool,
"eve_input_") ||
240 startsWith(tool,
"eve_ui_")) &&
241 (profile ==
"content.scene" || profile ==
"content.map" || profile ==
"script.bind" ||
242 profile ==
"fix.compile")) {
243 hint.
to =
"verify.play";
244 hint.
reason =
"play/observe verification requires verify.play";
247 if ((tool ==
"eve_api_search" || tool ==
"eve_api_get") &&
248 (profile ==
"content.scene" || profile ==
"content.map" || profile ==
"verify.play")) {
249 hint.
to =
"script.bind";
250 hint.
reason =
"binding-contract lookup is opened by script.bind";
253 if ((tool ==
"eve_restart" || tool ==
"eve_error_slice") && profile !=
"fix.compile") {
254 hint.
to =
"fix.compile";
255 hint.
reason =
"compile/runtime repair tools require fix.compile";
262 std::lock_guard lock(g_mu);
263 g_processProfile = std::string(profile);
264 g_processLoaded =
true;
268 std::lock_guard lock(g_mu);
269 ensureProcessLoadedUnlocked();
270 return g_processProfile;
274 std::lock_guard lock(g_mu);
275 g_sessionProfile = std::string(profile);
279 std::lock_guard lock(g_mu);
280 return g_sessionProfile;
284 std::lock_guard lock(g_mu);
286 if (!allowed) ++g_stats.toolsFiltered;
287 if (offeredEscalate) ++g_stats.escalateCount;
288 g_stats.approxResultBytes +=
static_cast<std::uint64_t
>(resultBytes);
292 std::lock_guard lock(g_mu);
297 std::lock_guard lock(g_mu);
Named Agent task profiles that narrow the MCP tool surface.
bool mcpResolveTaskProfile(std::string_view taskKind, std::string_view profile, std::string *outProfile, std::string *outTaskKind, std::string *errorCode, std::string *errorMessage)
Resolve taskKind + optional profile into a concrete profile id.
std::string mcpProcessProfile()
Current process-level profile; empty when unset.
std::vector< std::string > mcpProfileToolsetTags(std::string_view profile)
Tag list advertised for EVE_MCP_TOOLSET when only a named profile is chosen.
std::vector< std::string > mcpToolTags(std::string_view name)
Classify a built-in or script tool into toolset tags.
std::string mcpProfileToolsetEnv(std::string_view profile)
Comma-joined toolset tags for env forwarding (eve-mcp --profile=).
void mcpSetProcessProfile(std::string_view profile)
Install or clear the process-level profile (EVE_MCP_PROFILE mirror for tests).
McpFilterStats mcpFilterStats()
Snapshot of proxy metrics since process start / last test reset.
void mcpFilterStatsResetForTest()
Test helper: zero proxy metrics.
bool mcpProfileAllowsTool(std::string_view profile, std::string_view tool)
Whether tool is allowed under a concrete named profile (empty = no profile gate).
void mcpFilterNoteCall(bool allowed, std::size_t resultBytes, bool offeredEscalate)
Record a tools/call attempt for proxy metrics.
std::string mcpSessionProfile()
Current session-level profile; empty when no session overlay.
std::optional< McpProfileEscalate > mcpProfileEscalateHint(std::string_view profile, std::string_view tool)
Suggest an escalate edge when tool is denied by profile.
void mcpSetSessionProfile(std::string_view profile)
Install or clear the session-level profile overlay.
Proxy counters for profile A/B comparisons (not billing truth).
Suggested profile change when a call is rejected by the active profile.