main.py•1.94 kB
# server.py
from mcp.server.fastmcp import FastMCP
import os
# Create an MCP server
mcp = FastMCP("AI Sticky Notes")
NOTES_FILE = os.path.join(os.path.dirname(__file__),"notes.txt")
def ensure_file():
if not os.path.exists(NOTES_FILE):
with open(NOTES_FILE, "w") as f:
f.write("")
@mcp.tool()
def add_note(message: str) -> str:
"""
Append a new note to the sticky note file.
Args:
message (str): The note to be added.
Returns:
str: Confirmation message indicating the note was saved
"""
ensure_file()
with open(NOTES_FILE, "a") as f:
f.write(message + "\n")
return "Note saved!"
@mcp.tool()
def read_notes() -> str:
"""
Read all notes from the sticky note file.
Returns:
str: All notes as a single string separated by line breaks.
If no notes exist, a default message is returned.
"""
ensure_file()
with open(NOTES_FILE, "r") as f:
content = f.read().strip()
return content or "No notes yet"
@mcp.resource("notes://latest")
def get_latest_note() -> str:
"""
Get the most recently added note from the sticky note file.
Returns:
str: The latest note or a default message if no notes exist.
If no notes exist, a default message is returned.
"""
ensure_file()
with open(NOTES_FILE, "r") as f:
lines = f.readlines()
return lines[-1].strip() if lines else "No notes yet"
@mcp.prompt()
def note_summary_prompt() -> str:
"""
Generate a Prompt asking the AI summarize all the current notes.
Returns:
str: A prompt asking that includes all notes and asks for a summary.
If no notes exist, a default message is returned.
"""
ensure_file()
with open(NOTES_FILE, "r") as f:
content = f.read().strip()
if not content:
return "No notes yet"
return f"Summarize the following notes: {content}"