AI便签工具

一个基于Model Context Protocol (MCP)的AI便签工具,允许AI应用读写本地便签文件,适用于自动化开发和文件管理场景。

  • by Akshat-kay
  • Python
  • 本地部署
  • 更新于
在 GitHub 查看 Akshat-kay/AI-Sticky-Notes-MCP

使用说明

AI便签纸MCP 🗒️🤖(注:MCP可能代表某种特定功能或模式,但在此上下文中未给出具体含义,因此直接保留原样)

一个微型的、边做边学的MCP服务器,你可以在几分钟内复制、运行并扩展它。

这个仓库展示了如何构建一个 模型上下文协议(MCP) 一个允许AI应用(如 克劳德桌面版) 在你的电脑上写笔记和读笔记它设计得故意小巧,以便任何人都能理解其运作机制并对其进行适应性调整 他们自己的自动化系统。


为什么选择MCP?

大型语言模型(LLMs)在语言方面表现出色,但它们无法自行访问您的文件或工具。 MCP(多用途指挥平台) 这是安全地赋予AI(人工智能)能力的标准方法 工具 读取文件、调用API、查询数据库。此项目提供了四个简单的工具:

  • add_note(message) — 在本地追加一行 notes.txt
  • read_notes() — 阅读所有笔记
  • notes_summary() — 返回一个简短且适合大型语言模型(LLM)的摘要
  • notes://latest — 一个返回资源的(方法/接口) 最后 注

把这个当作一个 模式;图案 实现一切的自动化:待办事项列表、文件工作流、脚本、API等。


快速入门(5-10分钟)

1) 要求

  • Python 3.10及以上版本
  • 紫外线 (推荐) pipx install uv 或者从Astral下载
  • Claude Desktop(或任何MCP客户端)

2) 克隆并安装

git clone https://github.com/Akshat-kay/AI-Sticky-Notes-MCP.git
cd AI-Sticky-Notes-MCP
uv sync      # installs deps using pyproject.toml / uv.lock

没有uv?使用pip:

python -m venv .venv && source .venv/bin/activate  # Windows: .venv\Scripts\activate
pip install -r <(uv pip compile pyproject.toml)     # or just: pip install fastmcp

3) 运行服务器

python main.py

它启动了一个MCP服务器 stdio(标准输入输出库) (无开放端口,无云端)。当需要时,您的MCP客户端将启动此进程。

4) 将其连接到Claude Desktop

  1. 打开 Claude Desktop → 设置 → 工具(MCP)

  2. 添加本地服务器

    • 命令: python
    • 参数: main.py
    • 工作目录: 仓库文件夹
  3. 保存后,再将服务器开启。

你会看到一些工具: 添加备注, 阅读笔记,以及资源 笔记://最新。


试试这些提示

  • 添加大型语言模型(LLM)事实

    add 10 random facts about how LLMs work to notes
    
  • 准备MCP面试(侧重于自动化)

    Add 8 scenario-based MCP interview prep notes focused on automation development.
    
  • 总结

    Give me a short summary of the LLM notes and the MCP interview notes separately,
    then add a few lines on how they are related.
    
  • 获取最后一条笔记(资源)

    Fetch resource: notes://latest
    

它是如何运作的(通俗易懂版)

  • FastMCP服务器一个用于注册的微型Python应用程序 工具 和 资源.
  • 工具 是AI可以调用的带有参数的函数。
  • 资源 只读URI是AI可以获取的(比如 notes://latest)。
  • 克劳德/MCP 客户端 自动发现工具并在运行前请求许可。

你可以重用的代码

以下是 最小模式 对于任何基于文件的工具,请将其复制到一个新的工具中并修改其内容。

from mcp.server.fastmcp import FastMCP
import os

mcp = FastMCP("Sticky Notes")
BASE = os.path.dirname(__file__)
NOTES_PATH = os.path.join(BASE, "notes.txt")

def ensure_file():
    os.makedirs(BASE, exist_ok=True)
    if not os.path.exists(NOTES_PATH):
        with open(NOTES_PATH, "w", encoding="utf-8") as f:
            f.write("")

@mcp.tool()
def add_note(message: str) -> str:
    """Append a note (one line) to notes.txt."""
    ensure_file()
    with open(NOTES_PATH, "a", encoding="utf-8") as f:
        f.write(message.rstrip() + "\n")
    return "note saved"

@mcp.tool()
def read_notes() -> str:
    """Return the full notes file."""
    ensure_file()
    with open(NOTES_PATH, "r", encoding="utf-8") as f:
        content = f.read().strip()
    return content or "no notes yet"

@mcp.tool()
def notes_summary() -> str:
    """Return a lightweight natural-language summary (LLM-friendly)."""
    ensure_file()
    with open(NOTES_PATH, "r", encoding="utf-8") as f:
        content = f.read().strip()
    return "there are no notes yet" if not content else f"Summary:\n{content[:2000]}"

@mcp.resource("notes://latest")
def latest_note():
    """Return the last line as a resource (bytes, mime)."""
    ensure_file()
    with open(NOTES_PATH, "r", encoding="utf-8") as f:
        lines = f.readlines()
    latest = lines[-1].strip() if lines else "no notes yet"
    return latest.encode("utf-8"), "text/plain"

if __name__ == "__main__":
    mcp.run()

根据您的自动化需求进行定制

  • 调用一个shell命令

    @mcp.tool()
    def run_script(name: str) -> str:
        # validate 'name' against an allow-list to avoid injection
        ...
    
  • 与API交谈

    @mcp.tool()
    def get_weather(city: str) -> dict:
        # use requests/httpx; return small JSON the LLM can reason about
        ...
    
  • 查询数据库

    @mcp.tool()
    def list_tasks(status: str = "open") -> list[dict]:
        # use parameterized queries; never string-format SQL
        ...
    

设计小贴士

  • 保持 输入小且明确 (该模型推理能力更强)。
  • 返回 紧凑的JSON或纯文本 (节省标记;更易于解析)。
  • 添加 docstrings(文档字符串)—大型语言模型(LLMs)通过阅读它们来学习如何使用您的工具。

安全与保障(复制这种思维方式)

  • 代码中无秘密从环境变量加载;提交一个 *.env.example。
  • 最小特权原则限制文件路径;拒绝 .. 或者绝对路径。
  • 输入验证长度限制、命令白名单、清理文件名。
  • 锁定依赖项提交 uv.lock启用 Dependabot 和 CI 审计。
  • 将数据与代码分离不要提交像……这样的运行时文件 notes.txt。
  • 以非管理员用户身份运行 在可能的情况下。

将这个添加到 .gitignore:

.venv/
__pycache__/
*.pyc
notes.txt
.env
*.log
.vscode/
.idea/
.DS_Store
Thumbs.db

故障排除(快速修复)

  • 在Claude中,“response was interrupted”翻译为“响应被中断” → 您的服务器可能已崩溃。

    • 确保你以……结尾 mcp.run() (不 main()),并且实际拨打 ensure_file()。
    • 跑 python main.py 在终端中查看错误。
  • 克劳德找不到服务器

    • 检查 工作目录 并且 参数(或论据) 在MCP设置中。
    • 在Windows上,确认 python 指向正确的解释器(py -V, where python)。
  • Unicode 或行尾看起来很奇怪

    • 总是用(某个程序/软件)打开文件 encoding="utf-8" 并使用 \n。

了解更多/下一步

  • 为笔记(JSON 行格式)添加标签和标题,并构建一个 搜索 工具。
  • 创建一个 调度器 带有安全提示的工具(例如,定时运行)。
  • 将内部系统封装在MCP工具之后,为AI提供“安全的手”。

做出贡献

欢迎提出问题和提交拉取请求(PRs)——请保留示例 小巧、安全且注释详尽 所以学习MCP的朋友们可以跟着一起学。

许可证

麻省理工学院(请随意在您的项目中重用)。