File size: 7,677 Bytes
a2c1a36
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# MCP Client Configuration Guide

This guide shows how to configure various MCP clients to connect to the Document-MCP server for Markdown note management.

## Overview

Document-MCP provides an MCP server that exposes tools for managing Markdown notes, similar to how [jupyter-mcp-server](https://github.com/datalayer/jupyter-mcp-server) provides tools for Jupyter notebooks. Instead of notebook cells, we work with Markdown files in a vault structure.

## Available MCP Tools

| Tool Name | Description |
|-----------|-------------|
| `list_notes` | List all notes in the vault (optionally filtered by folder) |
| `read_note` | Read a Markdown note with metadata and body |
| `write_note` | Create or update a note (auto-updates timestamps and search index) |
| `delete_note` | Delete a note and remove it from the index |
| `search_notes` | Full-text search with BM25 ranking and recency bonus |
| `get_backlinks` | List notes that reference the target note |
| `get_tags` | List all tags with associated note counts |

## Transport Modes

### 1. STDIO Transport (Local Development)

For local development with Claude Desktop, Cursor, or other MCP clients.

#### Using Python Module

```json
{
  "mcpServers": {
    "obsidian-docs": {
      "command": "python",
      "args": ["-m", "src.mcp.server"],
      "cwd": "/absolute/path/to/Document-MCP/backend",
      "env": {
        "LOCAL_USER_ID": "local-dev",
        "JWT_SECRET_KEY": "local-dev-secret-key-123",
        "VAULT_BASE_PATH": "/absolute/path/to/Document-MCP/data/vaults",
        "DB_PATH": "/absolute/path/to/Document-MCP/data/index.db",
        "PYTHONPATH": "/absolute/path/to/Document-MCP/backend",
        "FASTMCP_SHOW_CLI_BANNER": "false"
      }
    }
  }
}
```

#### Using uv (Recommended)

```json
{
  "mcpServers": {
    "obsidian-docs": {
      "command": "uv",
      "args": ["run", "python", "-m", "src.mcp.server"],
      "cwd": "/absolute/path/to/Document-MCP/backend",
      "env": {
        "LOCAL_USER_ID": "local-dev",
        "JWT_SECRET_KEY": "local-dev-secret-key-123",
        "VAULT_BASE_PATH": "/absolute/path/to/Document-MCP/data/vaults",
        "DB_PATH": "/absolute/path/to/Document-MCP/data/index.db"
      }
    }
  }
}
```

### 2. HTTP Transport (Remote/Hosted)

For remote access via HTTP endpoint (e.g., Hugging Face Spaces deployment).

#### Configuration

```json
{
  "mcpServers": {
    "obsidian-docs": {
      "transport": "http",
      "url": "https://your-space.hf.space/mcp",
      "headers": {
        "Authorization": "Bearer YOUR_JWT_TOKEN"
      }
    }
  }
}
```

#### Obtaining a JWT Token

1. **Via OAuth (Production)**: Login through Hugging Face OAuth at `/auth/login`
2. **Via API (Development)**: 
   ```bash
   curl -X POST http://localhost:8001/api/tokens \
     -H "Authorization: Bearer YOUR_EXISTING_TOKEN"
   ```

## Client-Specific Configuration

### Claude Desktop

**Location**: `~/Library/Application Support/Claude/claude_desktop_config.json` (macOS) or `%APPDATA%\Claude\claude_desktop_config.json` (Windows)

**STDIO Example**:
```json
{
  "mcpServers": {
    "obsidian-docs": {
      "command": "uv",
      "args": ["run", "python", "-m", "src.mcp.server"],
      "cwd": "C:\\Workspace\\Document-MCP\\backend",
      "env": {
        "LOCAL_USER_ID": "local-dev",
        "JWT_SECRET_KEY": "local-dev-secret-key-123",
        "VAULT_BASE_PATH": "C:\\Workspace\\Document-MCP\\data\\vaults",
        "DB_PATH": "C:\\Workspace\\Document-MCP\\data\\index.db"
      }
    }
  }
}
```

**HTTP Example**:
```json
{
  "mcpServers": {
    "obsidian-docs": {
      "transport": "http",
      "url": "http://localhost:8001/mcp",
      "headers": {
        "Authorization": "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9..."
      }
    }
  }
}
```

### Cursor / VS Code with MCP Extension

**Location**: `.cursor/mcp.json` or `.vscode/mcp.json`

```json
{
  "mcpServers": {
    "obsidian-docs": {
      "command": "uv",
      "args": ["run", "python", "-m", "src.mcp.server"],
      "cwd": "${workspaceFolder}/backend",
      "env": {
        "LOCAL_USER_ID": "local-dev",
        "JWT_SECRET_KEY": "local-dev-secret-key-123",
        "VAULT_BASE_PATH": "${workspaceFolder}/data/vaults",
        "DB_PATH": "${workspaceFolder}/data/index.db"
      }
    }
  }
}
```

### Cline (VS Code Extension)

Similar to Cursor configuration, but may require restarting VS Code after changes.

## Environment Variables

| Variable | Description | Required | Default |
|----------|-------------|----------|---------|
| `LOCAL_USER_ID` | User ID for local development | No | `local-dev` |
| `JWT_SECRET_KEY` | Secret key for JWT token signing | Yes (HTTP mode) | - |
| `VAULT_BASE_PATH` | Base directory for user vaults | Yes | - |
| `DB_PATH` | Path to SQLite database | Yes | - |
| `FASTMCP_SHOW_CLI_BANNER` | Show FastMCP banner on startup | No | `true` |

## Testing the Connection

### Test STDIO Mode

```bash
cd backend
uv run python -m src.mcp.server
```

You should see:
```
MCP server starting in STDIO mode...
Listening for MCP requests on stdin/stdout...
```

### Test HTTP Mode

1. Start the FastAPI server (port 8000):
   ```bash
   cd backend
   uv run uvicorn main:app --host 0.0.0.0 --port 8000
   ```

2. Start the MCP server in HTTP mode (port 8001) in a **separate** terminal:
   ```bash
   cd backend
   MCP_TRANSPORT=http MCP_PORT=8001 python -m src.mcp.server
   ```

3. Test the MCP endpoint:
   ```bash
   curl -X POST http://localhost:8001/mcp \
     -H "Authorization: Bearer YOUR_TOKEN" \
     -H "Content-Type: application/json" \
     -d '{
       "jsonrpc": "2.0",
       "id": 1,
       "method": "tools/list",
       "params": {}
     }'
   ```

## Troubleshooting

### "Module not found" errors

- Ensure `PYTHONPATH` includes the backend directory
- Install dependencies: `cd backend && uv pip install -e .`
- Use `uv run` to ensure the virtual environment is activated

### "Authorization required" errors

- For HTTP mode, ensure you're sending a valid JWT token
- Generate a token: `POST /api/tokens` (requires authentication)
- For local development, use STDIO mode with `LOCAL_USER_ID`

### Connection refused

- Ensure the server is running on the expected port
- Check firewall settings
- For HTTP mode, verify the URL is correct

### Tools not appearing in client

- Restart the MCP client after configuration changes
- Check the client logs for error messages
- Verify the `cwd` path is absolute and correct
- Ensure all environment variables are set

## Best Practices

1. **Use absolute paths** for `cwd` and file paths in configuration
2. **Separate environments**: Use different `LOCAL_USER_ID` values for different projects
3. **Token security**: Never commit JWT tokens to version control
4. **Path handling**: Use forward slashes in JSON config (even on Windows)
5. **Testing**: Test STDIO mode locally before deploying HTTP mode

## Advanced: Docker Deployment

For production deployments, you can containerize the MCP server:

```dockerfile
FROM python:3.11-slim

WORKDIR /app
COPY backend/ ./backend/
RUN cd backend && pip install uv && uv pip install -e .

ENV LOCAL_USER_ID=default
ENV VAULT_BASE_PATH=/data/vaults
ENV DB_PATH=/data/index.db

CMD ["python", "-m", "backend.src.mcp.server"]
```

Then configure clients to use Docker:

```json
{
  "mcpServers": {
    "obsidian-docs": {
      "command": "docker",
      "args": [
        "run", "-i", "--rm",
        "-v", "/path/to/data:/data",
        "-e", "LOCAL_USER_ID",
        "-e", "JWT_SECRET_KEY",
        "your-image:latest"
      ],
      "env": {
        "LOCAL_USER_ID": "local-dev",
        "JWT_SECRET_KEY": "your-secret"
      }
    }
  }
}
```