-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathrun_server.py
More file actions
132 lines (116 loc) · 3.53 KB
/
Copy pathrun_server.py
File metadata and controls
132 lines (116 loc) · 3.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#!/usr/bin/env python
"""
Primary entry point to run the MCP filesystem server.
Usage: uv run run_server.py [dir1] [dir2] ... [options]
For use with MCP Inspector or Claude Desktop:
- Command: uv
- Arguments: --directory /path/to/mcp-filesystem run run_server.py [dir1] [dir2]
This simplified approach eliminates the need for module invocation with -m flag.
"""
import sys
import os
import typer
from typing import List, Optional
from typing_extensions import Annotated
from mcp_filesystem.server import load_config, mcp
app = typer.Typer(
name="mcp-filesystem",
help="MCP Filesystem Server",
add_completion=False,
)
@app.callback(invoke_without_command=True)
def main(
directories: Annotated[
Optional[List[str]],
typer.Argument(
help="Allowed directories (defaults to current directory if none provided)",
show_default=False,
),
] = None,
transport: Annotated[
Optional[str],
typer.Option(
"--transport",
"-t",
help="Transport protocol to use",
),
] = None,
host: Annotated[
Optional[str],
typer.Option(
"--host",
help="Host for SSE transport",
),
] = None,
port: Annotated[
Optional[int],
typer.Option(
"--port",
"-p",
help="Port for SSE transport",
),
] = None,
debug: Annotated[
bool,
typer.Option(
"--debug",
"-d",
help="Enable debug logging",
),
] = False,
version: Annotated[
bool,
typer.Option(
"--version",
"-v",
help="Show version information",
),
] = False,
) -> None:
"""Run the MCP Filesystem Server.
By default, the server will only allow access to the current directory.
You can specify one or more allowed directories as arguments.
"""
if version:
show_version()
return
# Load defaults from config
config = load_config()
mcp_cfg = config.get("mcp", {})
transport = (transport or mcp_cfg.get("transport") or "sse").lower()
host = host or mcp_cfg.get("host") or "0.0.0.0"
port = port if port is not None else int(mcp_cfg.get("port") or 18089)
# Set allowed directories in environment for the server to pick up
if directories:
os.environ["MCP_ALLOWED_DIRS"] = os.pathsep.join(directories)
# Set debug mode if requested
if debug:
os.environ["FASTMCP_LOG_LEVEL"] = "DEBUG"
try:
if transport == "sse":
os.environ["FASTMCP_PORT"] = str(port)
os.environ["FASTMCP_HOST"] = host
# Update settings explicitly because they were initialized at import time
if hasattr(mcp, "settings"):
mcp.settings.port = port
mcp.settings.host = host
mcp.run(transport="sse")
else:
mcp.run(transport="stdio")
except KeyboardInterrupt:
print("\nShutting down...", file=sys.stderr)
sys.exit(0)
except Exception as e:
print(f"Error: {e}", file=sys.stderr)
sys.exit(1)
def show_version() -> None:
"""Show version information."""
try:
from importlib.metadata import version as get_version
version = get_version("mcp-filesystem")
except ImportError:
version = "unknown"
print(f"MCP Filesystem Server v{version}")
print("A Model Context Protocol server for filesystem operations")
if __name__ == "__main__":
app()