main.py•3.75 kB
import asyncio
import json
import os
from pathlib import Path
from typing import Dict, Any, Optional, List
from datetime import datetime
import logging
from mcp.server.fastmcp import FastMCP
logging.basicConfig(
level=logging.DEBUG,
filename="mcp_server.log",
format='%(asctime)s - %(levelname)s - %(message)s'
)
class NotesMCPServer:
def __init__(self, notes_dir: str):
self.notes_dir = Path(notes_dir)
self.notes_dir.mkdir(parents=True, exist_ok=True)
# Create FastMCP server
self.mcp = FastMCP("Notes Server")
# Add tools
@self.mcp.tool()
async def create_note(title: str, content: str = "") -> Dict[str, Any]:
"""Create a new markdown note in the directory"""
if not title:
raise ValueError("Title is required")
filename = f"{title}.md"
filepath = self.notes_dir / filename
if filepath.exists():
raise ValueError(f"Note with title '{title}' already exists")
filepath.write_text(content, encoding='utf-8')
logging.info(f"Created note: {title}")
return {
"filename": filename,
"path": str(filepath),
"created_at": datetime.now().isoformat(),
}
@self.mcp.tool()
async def read_note(title: str) -> Dict[str, Any]:
"""Read an existing markdown note from the directory"""
if not title:
raise ValueError("Title is required")
filename = f"{title}.md"
filepath = self.notes_dir / filename
if not filepath.exists():
raise ValueError(f"Note with title '{title}' not found")
content = filepath.read_text(encoding='utf-8')
stats = filepath.stat()
return {
"title": title,
"content": content,
"path": str(filepath),
"created_at": datetime.fromtimestamp(stats.st_ctime).isoformat(),
"modified_at": datetime.fromtimestamp(stats.st_mtime).isoformat(),
}
@self.mcp.tool()
async def update_note(title: str, content: str = "") -> Dict[str, Any]:
"""Update an existing markdown note in the directory"""
if not title:
raise ValueError("Title is required")
filename = f"{title}.md"
filepath = self.notes_dir / filename
if not filepath.exists():
raise ValueError(f"Note with title '{title}' not found")
filepath.write_text(content, encoding='utf-8')
logging.info(f"Updated note: {title}")
return {
"filename": filename,
"path": str(filepath),
"updated_at": datetime.now().isoformat(),
}
@self.mcp.tool()
async def list_notes() -> Dict[str, Any]:
"""List all markdown notes in the diretory"""
notes = []
for filepath in self.notes_dir.glob("*.md"):
stats = filepath.stat()
notes.append({
"title": filepath.stem,
"path": str(filepath),
"created_at": datetime.fromtimestamp(stats.st_ctime).isoformat(),
"modified_at": datetime.fromtimestamp(stats.st_mtime).isoformat(),
})
return {
"notes": notes,
"total": len(notes),
}
def main():
notes_dir = r"C:\"
server = NotesMCPServer(notes_dir)
# Start the server
server.mcp.run()
if __name__ == "__main__":
main()