Repository navigation
Expand file tree
/
Copy pathMCP_CODE_REVIEW.html
More file actions
262 lines (242 loc) · 12.3 KB
/
Copy pathMCP_CODE_REVIEW.html
File metadata and controls
262 lines (242 loc) · 12.3 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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>MCP Code Review - AppFlowy Cloud Server</title>
<style>
* { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif; background: #0f172a; color: #e2e8f0; line-height: 1.6; padding: 2rem; }
.container { max-width: 1200px; margin: 0 auto; }
h1 { font-size: 2rem; margin-bottom: 0.5rem; color: #38bdf8; }
h2 { font-size: 1.4rem; margin: 2rem 0 1rem; color: #7dd3fc; border-bottom: 1px solid #334155; padding-bottom: 0.5rem; }
h3 { font-size: 1.1rem; margin: 1rem 0 0.5rem; color: #94a3b8; }
.subtitle { color: #94a3b8; margin-bottom: 2rem; }
.card { background: #1e293b; border-radius: 8px; padding: 1.5rem; margin-bottom: 1rem; border-left: 4px solid #334155; }
.card.critical { border-left-color: #ef4444; }
.card.warning { border-left-color: #f59e0b; }
.card.info { border-left-color: #3b82f6; }
.card.success { border-left-color: #22c55e; }
.badge { display: inline-block; padding: 0.15rem 0.5rem; border-radius: 4px; font-size: 0.75rem; font-weight: 600; text-transform: uppercase; }
.badge.critical { background: #7f1d1d; color: #fca5a5; }
.badge.warning { background: #78350f; color: #fcd34d; }
.badge.info { background: #1e3a5f; color: #93c5fd; }
code { background: #0f172a; padding: 0.15rem 0.4rem; border-radius: 4px; font-size: 0.85rem; color: #a5f3fc; }
pre { background: #0f172a; padding: 1rem; border-radius: 6px; overflow-x: auto; margin: 0.5rem 0; font-size: 0.85rem; }
pre code { background: none; padding: 0; }
.grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(350px, 1fr)); gap: 1rem; margin: 1rem 0; }
table { width: 100%; border-collapse: collapse; margin: 1rem 0; }
th, td { padding: 0.75rem; text-align: left; border-bottom: 1px solid #334155; }
th { background: #0f172a; color: #7dd3fc; font-weight: 600; }
.comparison { display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.comparison .pro { border-left: 3px solid #22c55e; }
.comparison .con { border-left: 3px solid #ef4444; }
ul { padding-left: 1.5rem; }
li { margin: 0.3rem 0; }
.highlight { background: #1e3a5f; padding: 0.2rem 0.4rem; border-radius: 3px; }
</style>
</head>
<body>
<div class="container">
<h1>MCP Code Review - AppFlowy Cloud Server</h1>
<p class="subtitle">Analysis of error handling, agent improvements, and auth patterns</p>
<h2>1. Critical Issues Found</h2>
<div class="card critical">
<span class="badge critical">Critical</span>
<h3>Double Client Initialization</h3>
<p><code>main.py:24-34</code> - The code creates an <code>AppFlowy</code> client twice: once inside an <code>if os.getenv("BASE_URL")</code> block, then again unconditionally. This means the custom base_url is always overwritten.</p>
<pre><code>if os.getenv("BASE_URL"):
client = AppFlowy(base_url=base_url, ...)
client = AppFlowy(...) # Overwrites the above!</code></pre>
</div>
<div class="card critical">
<span class="badge critical">Critical</span>
<h3>No Token Auto-Refresh</h3>
<p><code>main.py:37-41</code> - <code>_require_access_token()</code> only checks if a token exists, not if it's expired. Agents will get cryptic 401 errors instead of automatic refresh.</p>
</div>
<div class="card warning">
<span class="badge warning">Warning</span>
<h3>Generic Exception Handling</h3>
<p>All 25+ tools catch <code>Exception</code> broadly. This loses:</p>
<ul>
<li>HTTP status codes (401 vs 403 vs 404 vs 500)</li>
<li>API error messages from AppFlowy</li>
<li>Stack trace context</li>
</ul>
</div>
<h2>2. Where Agents Can Help</h2>
<div class="grid">
<div class="card info">
<span class="badge info">Enhancement</span>
<h3>Auto-Login on Token Expiry</h3>
<p>Agents could automatically re-authenticate when receiving 401 errors, rather than failing immediately.</p>
</div>
<div class="card info">
<span class="badge info">Enhancement</span>
<h3>Retry with Exponential Backoff</h3>
<p>For 429 (rate limit) and 5xx errors, agents could implement retry logic instead of failing.</p>
</div>
<div class="card info">
<span class="badge info">Enhancement</span>
<h3>Input Validation</h3>
<p>Agents can validate workspace_id/database_id formats before making calls, reducing wasted API requests.</p>
</div>
<div class="card info">
<span class="badge info">Enhancement</span>
<h3>Caching</h3>
<p>Cache workspace lists and database fields to reduce redundant API calls.</p>
</div>
</div>
<h2>3. Improved Error Messages</h2>
<table>
<thead>
<tr>
<th>Current Error</th>
<th>Improved Error</th>
<th>Tool</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>"Not authenticated. Please login first."</code></td>
<td><code>"Authentication required. Token expired or not set. Call appflowy_login() with email/password to authenticate."</code></td>
<td>All tools</td>
</tr>
<tr>
<td><code>"Login failed: {str(e)}"</code></td>
<td><code>"Login failed: Invalid credentials or server unreachable. Check email/password. Error: {type(e).__name__}: {e}"</code></td>
<td>appflowy_login</td>
</tr>
<tr>
<td><code>"Missing required field: {key}"</code></td>
<td><code>"Missing required field '{key}' in request payload. Expected type: {expected_type}. Example: {example}"</code></td>
<td>_payload_str</td>
</tr>
<tr>
<td><code>"Failed to list workspaces: {str(e)}"</code></td>
<td><code>"Failed to list workspaces: {type(e).__name__}. This may indicate: invalid token (try refresh), network issue, or API downtime. Details: {e}"</code></td>
<td>appflowy_list_workspaces</td>
</tr>
<tr>
<td><code>"At least one row ID is required."</code></td>
<td><code>"No row IDs provided. Expected comma-separated string of UUIDs, e.g.: 'row-uuid-1, row-uuid-2'. Got: '{row_ids}'"</code></td>
<td>appflowy_get_row_details</td>
</tr>
</tbody>
</table>
<h2>4. Auth Token Pattern Analysis</h2>
<p style="margin: 1rem 0; color: #94a3b8;">You asked about the auth pattern: is storing tokens in-memory better than using an odd/expired token?</p>
<div class="comparison">
<div class="card pro">
<h3>Current Pattern: In-Memory Token Store</h3>
<ul>
<li><span class="highlight">Simplicity</span> - No external dependencies (Redis, DB)</li>
<li><span class="highlight">Speed</span> - Instant access, no I/O</li>
<li><span class="highlight">Security</span> - No tokens persisted to disk</li>
<li><span class="highlight">Stateless</span> - Good for short-lived MCP sessions</li>
</ul>
</div>
<div class="card con">
<h3>Current Pattern: In-Memory Token Store</h3>
<ul>
<li><span class="highlight">Lost on restart</span> - Must re-authenticate every session</li>
<li><span class="highlight">No refresh</span> - Doesn't auto-refresh expired tokens</li>
<li><span class="highlight">Single process</span> - Can't share across processes</li>
<li><span class="highlight">No expiry tracking</span> - Doesn't know when token expires</li>
</ul>
</div>
</div>
<h3>Verdict: In-Memory is Fine, But Needs Auto-Refresh</h3>
<div class="card success">
<p>The in-memory pattern is <strong>appropriate for MCP servers</strong> because:</p>
<ul>
<li>MCP sessions are typically short-lived</li>
<li>No external dependencies keeps deployment simple</li>
<li>Security is better (no tokens on disk)</li>
</ul>
<p style="margin-top: 1rem;"><strong>However, it needs:</strong></p>
<ul>
<li>Token expiry timestamp tracking</li>
<li>Auto-refresh before expiry (5 min buffer)</li>
<li>Fallback to re-login if refresh fails</li>
</ul>
</div>
<h2>5. Recommended Changes</h2>
<div class="card">
<h3>Add Custom Exception Hierarchy</h3>
<pre><code>class MCPError(Exception): pass
class AuthError(MCPError): pass
class TokenExpiredError(AuthError): pass
class ValidationError(MCPError): pass
class APIError(MCPError):
def __init__(self, status_code: int, message: str):
self.status_code = status_code
super().__init__(f"HTTP {status_code}: {message}")</code></pre>
</div>
<div class="card">
<h3>Add Token Expiry Check</h3>
<pre><code>def _require_access_token() -> str:
token = client.token_store.get_access_token()
if not token:
raise AuthError("No access token. Call appflowy_login() first.")
# Check if token is expired (if expiry info available)
if hasattr(client.token_store, 'token_expiry'):
if datetime.now() >= client.token_store.token_expiry:
raise TokenExpiredError("Token expired. Call appflowy_refresh_token().")
return token</code></pre>
</div>
<div class="card">
<h3>Fix Double Client Init</h3>
<pre><code>if os.getenv("BASE_URL"):
client = AppFlowy(
base_url=str(os.getenv("BASE_URL")),
email=os.getenv("APPFLOWY_EMAIL"),
password=os.getenv("APPFLOWY_PASSWORD"),
)
else:
client = AppFlowy(
email=os.getenv("APPFLOWY_EMAIL"),
password=os.getenv("APPFLOWY_PASSWORD"),
)</code></pre>
</div>
<h2>6. Agent-Specific Improvements</h2>
<div class="card">
<h3>Error Recovery Strategies</h3>
<table>
<thead>
<tr>
<th>Error Type</th>
<th>Agent Action</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>TokenExpiredError</code></td>
<td>Auto-call <code>appflowy_refresh_token()</code>, retry original operation</td>
</tr>
<tr>
<td><code>AuthError</code></td>
<td>Prompt user for credentials, call <code>appflowy_login()</code></td>
</tr>
<tr>
<td><code>APIError(404)</code></td>
<td>Suggest listing available resources (workspaces, databases)</td>
</tr>
<tr>
<td><code>APIError(429)</code></td>
<td>Wait and retry with exponential backoff</td>
</tr>
<tr>
<td><code>APIError(5xx)</code></td>
<td>Retry up to 3 times, then report server issue</td>
</tr>
<tr>
<td><code>ValidationError</code></td>
<td>Show expected format and examples to user</td>
</tr>
</tbody>
</table>
</div>
</div>
</body>
</html>