|
1 | 1 | """MCP Text Editor Server implementation.""" |
2 | 2 |
|
3 | 3 | import logging |
4 | | -import traceback |
5 | | -from collections.abc import Sequence |
6 | | -from typing import Any, List |
| 4 | +from typing import Sequence |
7 | 5 |
|
8 | | -from mcp.server import Server |
9 | | -from mcp.types import TextContent, Tool |
| 6 | +from mcp.server.fastmcp import FastMCP |
| 7 | +from mcp.types import TextContent |
10 | 8 |
|
11 | 9 | from .handlers import ( |
12 | 10 | AppendTextFileContentsHandler, |
|
22 | 20 | logging.basicConfig(level=logging.INFO) |
23 | 21 | logger = logging.getLogger("mcp-text-editor") |
24 | 22 |
|
25 | | -app = Server("mcp-text-editor") |
| 23 | +app = FastMCP("mcp-text-editor") |
26 | 24 |
|
27 | | -# Initialize tool handlers |
| 25 | +# Initialize handlers |
28 | 26 | get_contents_handler = GetTextFileContentsHandler() |
29 | 27 | patch_file_handler = PatchTextFileContentsHandler() |
30 | 28 | create_file_handler = CreateTextFileHandler() |
|
33 | 31 | insert_file_handler = InsertTextFileContentsHandler() |
34 | 32 |
|
35 | 33 |
|
36 | | -@app.list_tools() |
37 | | -async def list_tools() -> List[Tool]: |
38 | | - """List available tools.""" |
39 | | - return [ |
40 | | - get_contents_handler.get_tool_description(), |
41 | | - create_file_handler.get_tool_description(), |
42 | | - append_file_handler.get_tool_description(), |
43 | | - delete_contents_handler.get_tool_description(), |
44 | | - insert_file_handler.get_tool_description(), |
45 | | - patch_file_handler.get_tool_description(), |
46 | | - ] |
47 | | - |
48 | | - |
49 | | -@app.call_tool() |
50 | | -async def call_tool(name: str, arguments: Any) -> Sequence[TextContent]: |
51 | | - """Handle tool calls.""" |
52 | | - logger.info(f"Calling tool: {name}") |
53 | | - try: |
54 | | - if name == get_contents_handler.name: |
55 | | - return await get_contents_handler.run_tool(arguments) |
56 | | - elif name == create_file_handler.name: |
57 | | - return await create_file_handler.run_tool(arguments) |
58 | | - elif name == append_file_handler.name: |
59 | | - return await append_file_handler.run_tool(arguments) |
60 | | - elif name == delete_contents_handler.name: |
61 | | - return await delete_contents_handler.run_tool(arguments) |
62 | | - elif name == insert_file_handler.name: |
63 | | - return await insert_file_handler.run_tool(arguments) |
64 | | - elif name == patch_file_handler.name: |
65 | | - return await patch_file_handler.run_tool(arguments) |
66 | | - else: |
67 | | - raise ValueError(f"Unknown tool: {name}") |
68 | | - except ValueError: |
69 | | - logger.error(traceback.format_exc()) |
70 | | - raise |
71 | | - except Exception as e: |
72 | | - logger.error(traceback.format_exc()) |
73 | | - raise RuntimeError(f"Error executing command: {str(e)}") from e |
| 34 | +# Register tools |
| 35 | +@app.tool() |
| 36 | +async def get_text_file_contents(path: str) -> Sequence[TextContent]: |
| 37 | + """Get the contents of a text file.""" |
| 38 | + return await get_contents_handler.run_tool({"path": path}) |
| 39 | + |
| 40 | + |
| 41 | +@app.tool() |
| 42 | +async def patch_text_file_contents(path: str, content: str) -> Sequence[TextContent]: |
| 43 | + """Patch the contents of a text file.""" |
| 44 | + return await patch_file_handler.run_tool({"path": path, "content": content}) |
| 45 | + |
| 46 | + |
| 47 | +@app.tool() |
| 48 | +async def create_text_file(path: str) -> Sequence[TextContent]: |
| 49 | + """Create a new text file.""" |
| 50 | + return await create_file_handler.run_tool({"path": path}) |
| 51 | + |
| 52 | + |
| 53 | +@app.tool() |
| 54 | +async def append_text_file_contents(path: str, content: str) -> Sequence[TextContent]: |
| 55 | + """Append content to a text file.""" |
| 56 | + return await append_file_handler.run_tool({"path": path, "content": content}) |
| 57 | + |
| 58 | + |
| 59 | +@app.tool() |
| 60 | +async def delete_text_file_contents(path: str) -> Sequence[TextContent]: |
| 61 | + """Delete the contents of a text file.""" |
| 62 | + return await delete_contents_handler.run_tool({"path": path}) |
| 63 | + |
| 64 | + |
| 65 | +@app.tool() |
| 66 | +async def insert_text_file_contents( |
| 67 | + path: str, content: str, position: int |
| 68 | +) -> Sequence[TextContent]: |
| 69 | + """Insert content into a text file at a specific position.""" |
| 70 | + return await insert_file_handler.run_tool( |
| 71 | + {"path": path, "content": content, "position": position} |
| 72 | + ) |
74 | 73 |
|
75 | 74 |
|
76 | 75 | async def main() -> None: |
77 | 76 | """Main entry point for the MCP text editor server.""" |
78 | 77 | logger.info(f"Starting MCP text editor server v{__version__}") |
79 | | - try: |
80 | | - from mcp.server.stdio import stdio_server |
81 | | - |
82 | | - async with stdio_server() as (read_stream, write_stream): |
83 | | - await app.run( |
84 | | - read_stream, |
85 | | - write_stream, |
86 | | - app.create_initialization_options(), |
87 | | - ) |
88 | | - except Exception as e: |
89 | | - logger.error(f"Server error: {str(e)}") |
90 | | - raise |
| 78 | + await app.run() # type: ignore[func-returns-value] |
0 commit comments