include/entropic/mcp/servers/filesystem.h¶
include/entropic/mcp/servers/filesystem.h
Enums
PathAccess
What a filesystem tool is about to do with a path (v2.13.0).
Carried to the host's approver so it can say "wants to WRITE /etc/…", not just "wants /etc/…". read_file and list_directory read; write_file and edit_file write.
readwrite
OutsideRootVerdict
The host's answer to an OutsideRootRequest (v2.13.0).
no_approver is distinct from rejected so the refusal can tell the operator the truth: "nobody was asked" and "somebody said no" are fixed by different configuration.
approvedrejectedno_approver
Types
using entropic::OutsideRootApprover = typedef OutsideRootVerdict (*)( const OutsideRootRequest& request, void* user_data) hook point
Approver the server consults under allow_outside_root: optional .
- Parameters
request— The pending access (valid for the call only).user_data— Opaque pointer given with the approver.
- Returns
- The verdict for this one call.
entropic::FileAccessTracker
Tracks file read state for read-before-write enforcement.
void entropic::FileAccessTracker::record_read(const std::string &session, const std::string &path, size_t hash)
Record that session read a file.
Record that a session read a file, with its content hash. The tracker is what makes read-before-write enforceable: write_file and edit_file refuse an existing file with no recorded read. gh#158 (v2.13.0): recorded under the READING session's key, so the read unlocks that session's writes and nobody else's; the map is locked because concurrent sessions record from several run threads.
- Parameters
session— Session key the read belongs to ("" = default).path— Canonical file path.hash— Content hash at time of read.session— Session key the read belongs to.
- threadsafety
- Safe from any thread (gh#158).
bool entropic::FileAccessTracker::was_read(const std::string &session, const std::string &path) const
Check whether session has read a file.
Check whether a session has read a file. gh#158 (v2.13.0): consults ONLY session's reads. A lookup never creates an entry, so asking on behalf of a session that read nothing does not grow the map.
- Parameters
session— Session key asking.path— Canonical file path.
- Returns
- true only when THIS session recorded a read of path.
- true when this session recorded a read of this path, regardless of whether the content has since changed.
- threadsafety
- Safe from any thread (gh#158).
bool entropic::FileAccessTracker::release_session(const std::string &session)
Forget every read session recorded (gh#158).
Forget every read a session recorded (gh#158). Called when the session's conversation ends — the model no longer holds the content it read, so a later write must read again.
- Parameters
session— Session key.
- Returns
- true when the session had recorded reads.
- threadsafety
- Safe from any thread.
std::size_t entropic::FileAccessTracker::session_count() const
Sessions currently holding recorded reads.
Sessions currently holding recorded reads (gh#158).
- Returns
- Session count.
entropic::FilesystemServer
Filesystem MCP server with read-before-write enforcement.
Inherits from entropic::MCPServerBase, entropic::SessionStateOwner.
entropic::FilesystemServer::FilesystemServer(const std::filesystem::path &root_dir, const FilesystemConfig &config, const std::string &data_dir, int model_context_bytes=0)
Construct with root directory, config, and data dir.
Construct filesystem server, create and register all tools. Issue #15 (v2.1.4): also loads .gitignore + .explorerignore via the IgnoreMatcher member so glob/grep/read filter ignored paths.
- Parameters
root_dir— Project root directory.config— Filesystem configuration.data_dir— Path to bundled data directory (for tool JSONs).model_context_bytes— Model context window in bytes (for size gate).root_dir— Project root directory — canonicalised here, and the boundary every tool's paths are confined to.data_dir— Path to bundled data directory.model_context_bytes— Model context window in bytes.
entropic::FilesystemServer::~FilesystemServer() override
Destructor (default, unique_ptr cleanup).
bool entropic::FilesystemServer::skip_duplicate_check(const std::string &tool_name) const override
read_file must always execute (updates FileAccessTracker ).
read_file always executes (updates FileAccessTracker). read_file must run even when repeated, because its side effect — recording the read in the tracker — is what unlocks a later write.
- Parameters
tool_name— Tool name.tool_name— Tool name to check.
- Returns
- true for read_file.
- true for "read_file", false for every other filesystem tool, which stay duplicate-checked.
bool entropic::FilesystemServer::set_working_dir(const std::string &path) override
Set working directory (changes root_dir).
Set working directory by updating root_dir. Issue #15 (v2.1.4): also reloads IgnoreMatcher rules so the new root's .gitignore + .explorerignore take effect immediately.
- Parameters
path— New root directory.
- Returns
- true on success.
- true when the path is a directory, in which case the server is re-rooted AND the ignore rules are reloaded from the new root; false when it is not, leaving the server untouched.
const fs::path & entropic::FilesystemServer::root_dir() const
Get the root directory.
- Returns
- Root directory path.
- The canonical root every requested path is resolved against and confined to.
FileAccessTracker & entropic::FilesystemServer::tracker()
Get the file access tracker.
- Returns
- Tracker reference.
- Mutable reference to the read-before-write tracker shared by read_file (which records) and write_file (which enforces), both under the calling session's key (gh#158).
bool entropic::FilesystemServer::release_session(const std::string &key) override
Release key 's recorded reads (gh#158).
Release a session's recorded reads (gh#158).
- Parameters
key— Session key.
- Returns
- true when the session had recorded reads.
std::size_t entropic::FilesystemServer::session_count() const override
Sessions whose reads this server currently tracks (gh#158).
Sessions whose reads this server tracks (gh#158).
- Returns
- Session count.
const FilesystemConfig & entropic::FilesystemServer::config() const
Get the filesystem config.
- Returns
- Config reference.
int entropic::FilesystemServer::max_read_bytes() const
Get max read bytes (size gate).
Get max read bytes for size gate.
- Returns
- Max bytes, or 0 for unlimited.
- The read size limit in bytes, or 0 for unlimited — a file over the limit is refused with size_exceeded.
fs::path entropic::FilesystemServer::resolve_path(const std::string &requested, PathAccess access, const std::string &tool) const
Resolve a path and apply the outside-root policy (v2.13.0).
Resolve a path and apply the outside-root policy. The single confinement point for every path-taking tool. A path under the root is served. One outside it is decided by, in order: outside_root_deny, outside_root_allow, then allow_outside_root (false refuse / true serve / optional ask the approver). Resolves relative paths against root_dir_ and canonicalizes. A result under the root is served. One outside it goes to authorize_outside_root, which throws on any refusal. Containment uses lexically_relative so that "/home/user/project" does not falsely contain "/home/user/projectile" via string-prefix — and the SAME test decides the outside-root allow/deny subtrees. The single confinement point every path-taking filesystem tool goes through, so no tool can reach outside the project without the policy saying so.
- Parameters
requested— User-requested path (absolute or root-relative).access— Whether the calling tool reads or writes it.tool— Bare tool name, for the approval request and logs.requested— User-requested path string (absolute or relative to the root).access— Whether the calling tool reads or writes the path.tool— Bare calling tool name (e.g. "write_file").
- Returns
- Resolved canonical path.
- The canonical path when it lies under the root, or when the outside-root policy served it.
- Throws
std::runtime_error— on any refusal — thrown rather than returned so MCPServerBase's barrier also skips the ContextAnchor for a refused read.std::runtime_error— when the policy refuses the path — thrown, so MCPServerBase's barrier returns it as a tool error AND skips the ContextAnchor a refused read must not get.
void entropic::FilesystemServer::set_outside_root_approver(OutsideRootApprover fn, void *user_data)
Install the approver consulted under optional (v2.13.0).
NOT installed on a named workspace's servers, which are confined unconditionally (gh#166). Thread-safe against a concurrent call.
- Parameters
fn— Approver, or nullptr to clear (escapes then refuse with outside_root_approval_required).user_data— Forwarded to fn.fn— Approver, or nullptr to clear.
const IgnoreMatcher & entropic::FilesystemServer::ignore() const
Get the ignore matcher (#15, v2.1.4).
Loaded from .gitignore (recursive) + .explorerignore at server construction and on set_working_dir. Used by glob, grep, and read_file to filter results / refuse access.
- Returns
- Matcher reference.
- Read-only reference to the loaded .gitignore + .explorerignore rule set that glob, grep and read_file all consult.
entropic::OutsideRootRequest
One outside-root access awaiting the host's decision (v2.13.0).
std::string entropic::OutsideRootRequest::path
Canonical absolute path the tool resolved.
std::string entropic::OutsideRootRequest::root
Canonical root the path lies outside.
std::string entropic::OutsideRootRequest::tool
Fully-qualified tool ("filesystem.write_file")
PathAccess entropic::OutsideRootRequest::access
Read or write.