Skip to main content
Glama
hesreallyhim

MCP Observer Server

by hesreallyhim

mcp-观察者-服务器

mcp-observer-server是一个 MCP(模型上下文协议)服务器,用于监控文件系统事件并向 MCP 客户端提供实时通知。它充当本地文件系统和 AI 助手(例如克劳德检查器,使他们能够自动响应文件更改。

**注意:**这是我正在开发的文件监控 MCP 服务器的演示/POC。我看到很多关于这类问题的问题/评论/问题/讨论,所以我想发布这个最小实现来分享我的方法。

语境

MCP 协议定义了资源订阅的概念,客户端可以请求接收资源任何变更的通知,服务器可以选择发送通知。流程图如下:

资源订阅流程图

协议规定客户端应该向服务器发送读取请求以读取更改。(顺便说一下,所有这些都是可选的)。但是,我觉得这有点麻烦,而且需要额外的一次传输,我更希望我的资源更新通知也能描述更改。幸运的是,SDK 提供了一个meta / _meta字段,你几乎可以发送任何你想要的内容。所以我可能想发送更改的行数、更改的差异,或者其他什么,谁知道呢。我在这个演示中没有实现这些,现在我只发送时间戳。(我基本上把服务器里除了最小 POC 之外的所有内容都删掉了。)而且,它只是在 stdio 传输上运行,没什么特别的。

**注意!!!**我还没有用任何“真正的” MCP 客户端测试过这一点——我的理解是,每个视图客户端实际上都支持资源订阅,因为它本来就是可选的。不过幸运的是, Inspector是一款非常优秀的客户端,你可以用它来测试这个服务器。

演示说明:

  1. 克隆存储库。

  2. 使用uv安装依赖项(或者,我想是其他方式)。

  3. 使用make start (使用uv )运行服务器或运行npx @modelcontextprotocol/inspector uv run src/mcp_observer_server/server.py 。

  4. 打开 Inspector 客户端并使用 stdio 连接,无需配置。

  5. 使用subscribe工具来监控一个目录或文件,(或者,您可以运行“列出资源”,单击一个资源,然后单击“订阅”按钮来订阅它)。

  6. 默认情况下,服务器会在src/mcp_observer_server/watched.txt中公开一个名为watched.txt的文件(该文件是 .gitignored 文件,因此您必须创建它),但您也可以订阅其他文件。您可以使用subscribe_default工具订阅此文件。

  7. 修改watched.txt文件(或你订阅的任何文件),你应该会看到在 Inspector 的右下角面板中出现一条服务器通知。这就是 POC 的雏形。

Related MCP server: File MCP Server

演示可视化

  1. 启动服务器并连接 Inspector:启动服务器并连接

  2. 列出默认资源:列出资源

  3. 列出工具:列表工具

  4. 订阅默认文件:订阅默认文件

  5. 修改文件:修改文件

  6. 查看出现的通知:查看通知

🎉

服务器描述

MCP 观察服务器 (MCP Observer Server) 跟踪系统中文件和目录的变更,允许 MCP 客户端订阅这些事件,并在文件创建、修改、删除或移动时采取行动(当前演示版本处理修改事件)。该服务器实现了完整的模型上下文协议 (MCP) 规范,提供:

  • 实时文件监控:使用 Watchdog 库进行高效的文件系统观察

  • 订阅管理:创建、列出和取消任何路径的监控订阅

  • 变更历史记录:维护每个订阅的最近变更日志(演示中省略)

  • 文件和目录访问:通过 MCP 资源读取文件内容和目录列表

  • 无状态设计:客户端控制文件更改时发生的响应

主要特点

  • 订阅特定文件、目录或整个存储库的更改

  • 按文件模式或事件类型过滤事件(演示中省略)

  • 查询最近的更改,查看哪些文件受到影响(演示中省略)

  • 通过资源端点访问文件内容

  • 轻量级、高效的实现,依赖性最小

  • 与任何 MCP 兼容客户端简单集成(...支持资源订阅)

实际应用

我试图解决的主要痛点是,除非 Claude Code 接触到文件并自行写入更改,否则它根本不知道你的代码库/项目中发生了什么。(你知道那些通知吗?“文件自上次读取后已更改”?)拥有一个客户端或编码助手来实际监控你在项目中的操作,你不必把所有任务都委托给 Claude 让它知道发生了什么,这对我来说非常有用。一些实际应用包括:

  • 自动文档更新:使文档与代码更改保持同步 - 您更新一些代码,Claude 会收到更改通知,并且它会主动检查或更新文档字符串等。

  • 实时代码审查:在工作时获得代码更改的实时反馈,捕捉拼写错误、类型错误等,提供建议,真正的结对编程。

  • 测试自动化:当相关文件被修改时运行测试。

  • AI辅助:启用AI工具自动响应文件更改。

  • Git 提交自动化:你是否经常忘记提交?Claude 可以监控你的更改,并建议(或执行)更频繁的提交操作。

当前实施设计

服务器实现具有简化的架构,优先考虑简单性、可靠性和可维护性。

架构亮点

  1. 简化结构

    • 重点实施(约 170 行代码)

    • 将功能整合到一小组核心组件中

    • 简洁的基于函数的设计,直接利用 MCP SDK

    • 较高的可读性和可维护性

  2. 高效的状态管理

    • 简单的字典结构将路径映射到客户端会话

    • 使用watched字典进行直接路径到会话的映射

    • 具有清晰数据流的最小状态跟踪

    • 避免冗余数据结构

  3. MCP 协议集成

    • 直接使用 MCP SDK 函数装饰器

    • 清理资源 URI 处理

    • 通过适当的功能配置简化服务器初始化

    • 直接通知传递系统

  4. 事件处理

    • 简化的看门狗事件处理程序实现

    • 直接事件到通知路径

    • 通过call_soon_threadsafe实现线程安全通信

    • 高效的事件过滤

  5. 通知系统

    • 直接使用 MCP 通知原语

    • 通过适当的错误处理实现可靠的交付

    • 准确的 UTC 时间戳处理

    • 清理 URI 格式

核心组件

  1. 数据结构

    • watched单个全局字典将 Path 对象映射到 ServerSession 对象集

    • 每个路径条目包含订阅该路径的会话集

  2. 工具 API

    • 两个必备工具: subscribe和unsubscribe

    • 简单的路径参数,用于直接的订阅管理

    • 清理错误处理和路径验证

  3. 资源处理

    • 通过资源列表直接公开的文件 URI

    • 路径解析和验证

    • 文件的文本内容读取

  4. 事件处理

    • Watcher 类扩展了 FileSystemEventHandler

    • 直接处理修改的事件

    • 线程安全通知调度

    • 嵌套路径的路径相关性处理

  5. 通知传递

    • ServerNotification 创建和发送

    • 带有时间戳的事件元数据

    • 清理 URI 格式

该实现在功能性和简单性之间取得了良好的平衡,从而形成了可靠且可维护的代码库。

Available Tools

4 tools
list_watchedA

List all currently monitored paths and their subscriber counts

ParametersJSON Schema
NameRequiredDescriptionDefault

No parameters

TDQS

A3.6/5.0
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It indicates a read operation ('List') but doesn't specify whether this requires authentication, how data is returned (e.g., format, pagination), or any rate limits. The description is minimal and lacks essential behavioral context for a tool that likely interacts with subscription systems.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that front-loads the core purpose without any wasted words. It directly communicates the tool's function in a clear and structured manner, making it easy to understand at a glance.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's complexity (likely low, but involves subscription monitoring), no annotations, and no output schema, the description is insufficient. It doesn't explain what the output looks like (e.g., list format, data structure), potential errors, or operational constraints, leaving significant gaps for an AI agent to use it effectively.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The tool has 0 parameters with 100% schema description coverage, so the schema fully documents the absence of inputs. The description doesn't need to compensate for any parameter gaps, and it appropriately doesn't mention parameters, making it complete in this regard. A baseline of 4 is appropriate for zero-parameter tools.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the specific action ('List all') and resource ('currently monitored paths and their subscriber counts'), distinguishing it from sibling tools like subscribe/unsubscribe which perform different operations. It precisely defines what the tool does without being vague or tautological.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines3/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description implies usage context by specifying 'currently monitored paths,' suggesting this tool is for viewing existing subscriptions rather than modifying them. However, it doesn't explicitly state when to use this versus alternatives or provide any exclusion criteria, leaving some ambiguity about its specific application scenarios.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

subscribeC

Subscribe to changes on a file or directory

ParametersJSON Schema
NameRequiredDescriptionDefault
pathYes

TDQS

C2.8/5.0
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. While 'Subscribe to changes' implies a monitoring/notification function, it doesn't describe what kind of changes trigger notifications, how notifications are delivered, whether this requires specific permissions, rate limits, or what happens when multiple subscriptions exist. This leaves significant behavioral gaps for a tool that likely establishes ongoing monitoring.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence that states the core purpose without unnecessary words. It's appropriately sized for a tool with one parameter and gets straight to the point with zero wasted verbiage.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

For a subscription tool with no annotations, no output schema, and minimal parameter documentation, the description is inadequate. It doesn't explain what 'subscribing' entails operationally, what format notifications take, how to manage subscriptions, or what the tool returns. Given the complexity of establishing monitoring and the complete lack of structured documentation, this description leaves too many questions unanswered.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters2/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

With 0% schema description coverage for the single 'path' parameter, the description provides no additional semantic information about what the path represents, its format, or constraints. The description mentions 'file or directory' which gives some context for the path parameter, but this is minimal compensation for the complete lack of schema documentation.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the action ('Subscribe to changes') and target resource ('on a file or directory'), providing a specific verb+resource combination. However, it doesn't distinguish this tool from its sibling 'subscribe_default', which appears to be a related subscription tool, so it doesn't fully differentiate from alternatives.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives like 'subscribe_default' or 'list_watched'. It doesn't mention prerequisites, exclusions, or contextual factors that would help an agent choose between subscription-related tools.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

subscribe_defaultB

Subscribe to the default watched.txt file for development

ParametersJSON Schema
NameRequiredDescriptionDefault

No parameters

TDQS

B3.1/5.0
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It states the action ('subscribe') but doesn't explain what subscription entails (e.g., real-time updates, notifications, persistence), permissions required, side effects, or error conditions. This leaves significant gaps for a mutation-like operation.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence with no wasted words. It front-loads the core action and target, making it easy to parse quickly. Every element ('subscribe', 'default', 'watched.txt file', 'development') contributes meaning without redundancy.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool has no parameters (simplifying input) but no annotations or output schema, the description is incomplete. It lacks details on behavior, return values, error handling, and differentiation from siblings like 'subscribe'. For a subscription tool with mutation implications, this leaves too many unknowns for effective agent use.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The tool has 0 parameters, and schema description coverage is 100% (empty schema). The description doesn't need to add parameter details, and it appropriately avoids discussing nonexistent inputs. A baseline of 4 is applied since no parameters exist to document.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the action ('subscribe') and target resource ('default watched.txt file for development'), making the purpose understandable. It doesn't explicitly distinguish from sibling tools like 'subscribe' (which likely allows custom targets) or 'list_watched'/'unsubscribe', but the specificity of 'default' provides some implicit differentiation.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

No explicit guidance is provided on when to use this tool versus alternatives like 'subscribe' (for non-default files) or 'list_watched' (for viewing subscriptions). The description implies it's for development purposes, but doesn't clarify prerequisites, exclusions, or specific use cases compared to siblings.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

unsubscribeC

Unsubscribe from changes on a file or directory

ParametersJSON Schema
NameRequiredDescriptionDefault
pathYes

TDQS

C2.8/5.0
Behavior2/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations provided, the description carries the full burden of behavioral disclosure. It states the action ('Unsubscribe from changes') but doesn't explain what 'changes' refers to, whether this operation is reversible, what permissions are required, or what happens after unsubscribing (e.g., notifications stop). For a mutation tool with zero annotation coverage, this leaves significant behavioral gaps.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is a single, efficient sentence with zero waste—it directly states the tool's purpose without unnecessary words. It's appropriately sized and front-loaded, making it easy to parse quickly.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness2/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's complexity (a mutation operation with no annotations, no output schema, and low schema coverage), the description is incomplete. It lacks details on behavioral traits, parameter usage, output expectations, and differentiation from siblings, making it inadequate for informed tool selection and invocation.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters2/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

The input schema has 1 parameter with 0% description coverage, so the schema provides no semantic information. The description mentions 'a file or directory' but doesn't clarify what the 'path' parameter represents (e.g., format, examples, or constraints). It adds minimal value beyond the schema's structural definition.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose4/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states the action ('Unsubscribe from changes') and the target resource ('on a file or directory'), providing a specific verb+resource combination. However, it doesn't explicitly distinguish this tool from its sibling 'subscribe' or 'subscribe_default', which would require mentioning what makes 'unsubscribe' different from those subscription tools.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines2/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides no guidance on when to use this tool versus alternatives like 'list_watched' or when not to use it. There's no mention of prerequisites (e.g., needing an existing subscription) or contextual cues for selection among sibling tools, leaving usage decisions ambiguous.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Tool Schema Changelog

Recent tool additions, removals, and schema changes observed during successful MCP inspections.

  1. 4 tool updates
    • First observedlist_watched
    • First observedsubscribe
    • First observedsubscribe_default
    • First observedunsubscribe

TDQS

A3.6/5.0

Scored across 4 tools

Disambiguation5/5

Each tool has a clearly distinct purpose with no overlap: list_watched for viewing current subscriptions, subscribe for adding new ones, subscribe_default for a specific default case, and unsubscribe for removal. The descriptions reinforce these distinct roles, making misselection unlikely.

Naming Consistency5/5

All tools follow a consistent verb_noun pattern (list_watched, subscribe, subscribe_default, unsubscribe) with clear, action-oriented names. The naming is uniform and predictable, enhancing usability.

Tool Count5/5

With 4 tools, this server is well-scoped for its purpose of monitoring file/directory changes. Each tool serves a necessary function in the subscription lifecycle, and the count is neither too sparse nor bloated.

Completeness5/5

The tool set provides complete coverage for the domain of file/directory monitoring: list (read), subscribe (create), unsubscribe (delete), and a specialized subscribe_default for convenience. There are no obvious gaps, supporting full agent workflows.

Maintenance

ActivityInactive
ResponsivenessNo issues

Related MCP Connectors

Related MCP Servers

  • F
    license
    A
    quality
    D
    maintenance
    A Model Context Protocol (MCP) server that enables AI assistants to perform comprehensive file operations including finding, reading, writing, editing, searching, moving, and copying files with security validations.
    7
    1
    -
  • A
    license
    Not graded
    quality
    C
    maintenance
    A secure file server for AI assistants that provides comprehensive file operations and text manipulation with configurable access levels and multiple connection modes.
    6
    MIT
  • F
    license
    Not graded
    quality
    D
    maintenance
    A secure, sandboxed file system server that enables reading, writing, searching, and managing files through MCP-compatible AI clients with path traversal protection and size limits.
    -