-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver.py
More file actions
1295 lines (1106 loc) · 42.3 KB
/
Copy pathserver.py
File metadata and controls
1295 lines (1106 loc) · 42.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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""Substack MCP Server — full Substack integration for Claude Code.
29 tools across 3 phases:
Phase 1: Notes publishing + vault pipeline
Phase 2: Post management + engagement
Phase 3: Analytics + scale features
Built with FastMCP v3. Auth via substack.sid session cookie.
Multi-publication support (switch between pubs per tool call).
"""
import json
import os
import re
from datetime import datetime, date
from pathlib import Path
from urllib.parse import urlparse
import httpx
from fastmcp import FastMCP
from mycelium_security import UnsafeURL, assert_public_ip, sanitize_or_raise
from prosemirror import md_to_prosemirror, md_to_note_body
# SSRF hardening (MYC-101): every outbound URL passes through _validate_url
# before any of the _get/_post/_put/_delete/_post_form helpers fire. Returns
# the safe URL string or raises UnsafeURL (callers convert to {"error": ...}
# so the never-raises contract of the fetch helpers is preserved).
def _validate_url(url: str) -> str:
safe = sanitize_or_raise(url)
host = urlparse(safe).hostname or ""
assert_public_ip(host)
return safe
# ---------------------------------------------------------------------------
# Server init
# ---------------------------------------------------------------------------
mcp = FastMCP(
"Substack",
instructions=(
"Substack integration: publish Notes, manage posts, pull analytics, "
"and bridge your Obsidian vault drafts directly to Substack."
),
)
CONFIG_PATH = Path(__file__).parent / "config.json"
GLOBAL_BASE = "https://substack.com/api/v1"
def _load_config() -> dict:
"""Load config from disk (re-read each call so edits take effect)."""
if not CONFIG_PATH.exists():
return {"publications": [], "default_publication": "main", "vault_drafts_path": ""}
return json.loads(CONFIG_PATH.read_text())
def _get_pub(name: str | None = None) -> dict:
"""Get publication config by name (or default)."""
cfg = _load_config()
target = name or cfg.get("default_publication", "main")
for pub in cfg.get("publications", []):
if pub["name"] == target:
return pub
if cfg.get("publications"):
return cfg["publications"][0]
raise ValueError("No publications configured. Copy config.example.json to config.json and fill in your publication(s).")
def _pub_base(pub: dict) -> str:
"""Base URL for a publication-scoped API."""
return f"https://{pub['subdomain']}.substack.com/api/v1"
def _headers(pub: dict) -> dict:
"""Auth headers for API calls."""
cookie = pub.get("cookie", "")
# Accept raw cookie value or full "substack.sid=..." format
if not cookie.startswith("substack.sid="):
cookie = f"substack.sid={cookie}"
return {
"Cookie": cookie,
"Accept": "application/json",
"Content-Type": "application/json",
"User-Agent": "SubstackMCP/1.0",
}
async def _get(url: str, pub: dict, params: dict | None = None) -> dict | list:
"""Authenticated GET."""
try:
url = _validate_url(url)
except UnsafeURL as exc:
return {"error": f"refused (SSRF): {exc}"}
async with httpx.AsyncClient(follow_redirects=False) as client:
r = await client.get(url, headers=_headers(pub), params=params, timeout=20)
if r.status_code in (401, 403):
return {"error": "Auth failed. Cookie may be expired. Re-extract from Chrome DevTools."}
if r.status_code >= 400:
try:
err_body = r.json()
except Exception:
err_body = r.text
return {"error": f"HTTP {r.status_code}", "response": err_body}
r.raise_for_status()
return r.json()
async def _post(url: str, pub: dict, body: dict | None = None) -> dict | list:
"""Authenticated POST (JSON)."""
try:
url = _validate_url(url)
except UnsafeURL as exc:
return {"error": f"refused (SSRF): {exc}"}
async with httpx.AsyncClient(follow_redirects=False) as client:
r = await client.post(url, headers=_headers(pub), json=body or {}, timeout=30)
if r.status_code in (401, 403):
return {"error": "Auth failed. Cookie may be expired. Re-extract from Chrome DevTools."}
if r.status_code >= 400:
# Return the error body so callers can surface what Substack said.
try:
err_body = r.json()
except Exception:
err_body = r.text
return {"error": f"HTTP {r.status_code}", "response": err_body}
r.raise_for_status()
return r.json()
async def _put(url: str, pub: dict, body: dict | None = None) -> dict | list:
"""Authenticated PUT (JSON)."""
try:
url = _validate_url(url)
except UnsafeURL as exc:
return {"error": f"refused (SSRF): {exc}"}
async with httpx.AsyncClient(follow_redirects=False) as client:
r = await client.put(url, headers=_headers(pub), json=body or {}, timeout=30)
if r.status_code in (401, 403):
return {"error": "Auth failed. Cookie may be expired. Re-extract from Chrome DevTools."}
if r.status_code >= 400:
try:
err_body = r.json()
except Exception:
err_body = r.text
return {"error": f"HTTP {r.status_code}", "response": err_body}
r.raise_for_status()
return r.json()
async def _delete(url: str, pub: dict) -> dict:
"""Authenticated DELETE."""
try:
url = _validate_url(url)
except UnsafeURL as exc:
return {"error": f"refused (SSRF): {exc}"}
async with httpx.AsyncClient(follow_redirects=False) as client:
r = await client.delete(url, headers=_headers(pub), timeout=20)
if r.status_code in (401, 403):
return {"error": "Auth failed. Cookie may be expired."}
r.raise_for_status()
return {"status": "deleted"}
async def _post_form(url: str, pub: dict, data: dict) -> dict:
"""Authenticated POST with form data (for image upload)."""
try:
url = _validate_url(url)
except UnsafeURL as exc:
return {"error": f"refused (SSRF): {exc}"}
headers = _headers(pub)
headers["Content-Type"] = "application/x-www-form-urlencoded"
async with httpx.AsyncClient(follow_redirects=False) as client:
r = await client.post(url, headers=headers, data=data, timeout=60)
if r.status_code in (401, 403):
return {"error": "Auth failed. Cookie may be expired."}
r.raise_for_status()
return r.json()
# ===========================================================================
# PHASE 1: Notes + Vault Pipeline
# ===========================================================================
@mcp.tool()
async def test_connection(publication: str | None = None) -> dict:
"""Verify auth works for a publication. Returns your profile info.
Args:
publication: Publication name from config (default: default_publication)
"""
pub = _get_pub(publication)
result = await _get(f"{GLOBAL_BASE}/user/profile/self", pub)
if isinstance(result, dict) and "error" in result:
return result
return {
"status": "connected",
"publication": pub["name"],
"subdomain": pub["subdomain"],
"user_id": result.get("id"),
"name": result.get("name"),
"email": result.get("email"),
"primary_publication": result.get("primaryPublication", {}).get("subdomain"),
}
@mcp.tool()
async def list_publications() -> list[dict]:
"""Show all configured publications and which is default."""
cfg = _load_config()
default = cfg.get("default_publication", "main")
return [
{
"name": p["name"],
"subdomain": p["subdomain"],
"is_default": p["name"] == default,
"has_cookie": bool(p.get("cookie")) and "PASTE" not in p.get("cookie", ""),
}
for p in cfg.get("publications", [])
]
@mcp.tool()
async def list_sections(publication: str | None = None) -> list[dict]:
"""List the sections configured for a publication.
Publications that have sections require every post to be filed under
one before publishing (the API returns "Please choose a section"
otherwise). Use the returned `id` as `section_id` in create_draft /
update_draft / publish_post / schedule_post.
Args:
publication: Publication name from config (default: default_publication)
Returns:
List of {id, name, slug, description, ...} for each section.
Empty list if the publication has no sections configured.
"""
pub = _get_pub(publication)
result = await _get(f"{_pub_base(pub)}/publication/sections", pub)
if isinstance(result, dict) and "error" in result:
return [result]
return result if isinstance(result, list) else []
@mcp.tool()
async def publish_note(
text: str,
publication: str | None = None,
attachment_ids: list[str] | None = None,
) -> dict:
"""Publish a Note to Substack, optionally with image or link attachments.
Args:
text: The note content (markdown supported: bold, italic, links, lists). Can be empty string for image-only notes.
publication: Publication name from config (default: default_publication)
attachment_ids: Optional list of attachment UUIDs previously created via create_note_attachment.
"""
pub = _get_pub(publication)
body_json = md_to_note_body(text or " ")
payload = {
"bodyJson": body_json,
"tabId": "for-you",
"surface": "feed",
"replyMinimumRole": "everyone",
"attachmentIds": attachment_ids or [],
}
result = await _post(f"{GLOBAL_BASE}/comment/feed/", pub, payload)
if isinstance(result, dict) and "error" in result:
return result
return {
"status": "published",
"note_id": result.get("id"),
"date": result.get("date"),
"url": f"https://substack.com/notes/post/p-{result.get('id', '')}",
"attachment_count": len(attachment_ids or []),
}
@mcp.tool()
async def create_note_attachment(
image_path: str | None = None,
image_url: str | None = None,
link_url: str | None = None,
publication: str | None = None,
) -> dict:
"""Create a Note attachment (image or link) and return its UUID for use with publish_note.
Provide exactly one of image_path, image_url, or link_url.
Flow for image attachment:
1. If image_path, upload to Substack CDN first via /image endpoint.
2. POST to /comment/attachment with {type: "image", imageUrl, imageWidth, imageHeight}.
3. Return the attachment UUID.
Args:
image_path: Local file path to an image to upload and attach.
image_url: Already-hosted image URL (skips upload step).
link_url: URL to attach as a link-type attachment.
publication: Publication name from config.
Returns:
dict with keys: attachment_id, type, and the original url/path.
"""
import base64
from PIL import Image as PILImage
pub = _get_pub(publication)
if link_url:
payload = {"type": "link", "url": link_url}
result = await _post(f"{GLOBAL_BASE}/comment/attachment", pub, payload)
if isinstance(result, dict) and "error" in result:
return result
return {"attachment_id": result.get("id"), "type": "link", "url": link_url}
# Image attachment path
if image_path:
path = Path(image_path).expanduser()
if not path.exists():
return {"error": f"File not found: {image_path}"}
# Detect dimensions
with PILImage.open(path) as im:
width, height = im.size
# Upload to Substack CDN first
suffix = path.suffix.lower()
mime_map = {".jpg": "jpeg", ".jpeg": "jpeg", ".png": "png", ".gif": "gif", ".webp": "webp"}
mime = mime_map.get(suffix, "png")
img_data = base64.b64encode(path.read_bytes()).decode("utf-8")
data_uri = f"data:image/{mime};base64,{img_data}"
upload_result = await _post_form(
f"{_pub_base(pub)}/image",
pub,
data={"image": data_uri},
)
if isinstance(upload_result, dict) and "error" in upload_result:
return upload_result
uploaded_url = upload_result.get("url", "")
if not uploaded_url:
return {"error": "CDN upload returned no URL", "detail": upload_result}
elif image_url:
uploaded_url = image_url
# Best-effort: probe dimensions if the URL is accessible
width, height = 1080, 1080
else:
return {"error": "Provide image_path, image_url, or link_url"}
# Create the note-scoped image attachment.
# IMPORTANT: endpoint REQUIRES trailing slash, and field is "url" not "imageUrl".
# Discovered 2026-04-17 after 500 errors with other shapes.
attach_payload = {
"type": "image",
"url": uploaded_url,
}
result = await _post(f"{GLOBAL_BASE}/comment/attachment/", pub, attach_payload)
if isinstance(result, dict) and "error" in result:
return result
return {
"attachment_id": result.get("id"),
"type": "image",
"image_url": uploaded_url,
"width": width,
"height": height,
}
@mcp.tool()
async def list_my_notes(
limit: int = 20,
publication: str | None = None,
) -> list[dict]:
"""Read your own recent Notes.
Args:
limit: Max notes to return (default 20)
publication: Publication name from config
"""
pub = _get_pub(publication)
# First get user ID
profile = await _get(f"{GLOBAL_BASE}/user/profile/self", pub)
if isinstance(profile, dict) and "error" in profile:
return [profile]
user_id = profile.get("id")
result = await _get(
f"{GLOBAL_BASE}/reader/feed/profile/{user_id}",
pub,
params={"types": "note", "limit": str(limit)},
)
if isinstance(result, dict) and "error" in result:
return [result]
items = result.get("items", [])
notes = []
for item in items[:limit]:
comment = item.get("comment", item)
notes.append({
"id": comment.get("id"),
"date": comment.get("date"),
"body_preview": (comment.get("body", "") or "")[:200],
"reactions": comment.get("reaction_count", 0),
"comments": comment.get("children_count", 0),
})
return notes
@mcp.tool()
async def reply_to_note(
note_id: int,
text: str,
publication: str | None = None,
) -> dict:
"""Reply to a Note by ID.
Args:
note_id: The ID of the note to reply to
text: Reply content (markdown supported)
publication: Publication name from config
"""
pub = _get_pub(publication)
body_json = md_to_note_body(text)
payload = {
"bodyJson": body_json,
"parentCommentId": note_id,
}
result = await _post(f"{GLOBAL_BASE}/comment/feed/", pub, payload)
if isinstance(result, dict) and "error" in result:
return result
return {
"status": "replied",
"reply_id": result.get("id"),
"parent_note_id": note_id,
}
def _parse_vault_drafts() -> list[dict]:
"""Parse the vault drafts file into individual drafts with metadata."""
cfg = _load_config()
drafts_path = Path(cfg.get("vault_drafts_path", "")).expanduser()
if not drafts_path.exists():
return []
content = drafts_path.read_text(encoding="utf-8")
# Find sections
sections = {"essay_seeds": [], "ready_to_post": [], "published": [], "other": []}
current_section = "other"
# Split by --- separators
raw_drafts = re.split(r"\n---+\n", content)
for chunk in raw_drafts:
chunk = chunk.strip()
if not chunk:
continue
# Detect section headers
lower = chunk.lower()
if "## essay seeds" in lower or "## essay seed" in lower:
current_section = "essay_seeds"
# Remove the header line and continue with remaining text
lines = chunk.split("\n")
chunk = "\n".join(line for line in lines if not line.strip().lower().startswith("## essay seed"))
chunk = chunk.strip()
if not chunk:
continue
elif "## ready to post" in lower:
current_section = "ready_to_post"
lines = chunk.split("\n")
chunk = "\n".join(line for line in lines if not line.strip().lower().startswith("## ready to post"))
chunk = chunk.strip()
if not chunk:
continue
elif "## published" in lower:
current_section = "published"
continue
elif chunk.startswith("# Substack Notes"):
continue
elif chunk.startswith("## Substack Notes Best"):
continue # Skip the best practices section
elif "best practices" in lower and len(chunk) > 500:
continue # Skip large best-practices blocks
# Extract source metadata
source_match = re.search(r"\*\(Source:.*?\)\*", chunk)
source = source_match.group(0) if source_match else None
# Extract title (first bold text or first sentence)
title_match = re.match(r"\*\*(.+?)\*\*", chunk)
if title_match:
title = title_match.group(1)
else:
first_line = chunk.split("\n")[0][:80]
title = first_line
sections[current_section].append({
"title": title,
"text": chunk,
"source": source,
"section": current_section,
})
# Combine essay_seeds and ready_to_post, then other
all_drafts = []
for i, d in enumerate(sections["ready_to_post"]):
d["index"] = i
d["section_label"] = "Ready to Post"
all_drafts.append(d)
for d in sections["essay_seeds"]:
d["index"] = len(all_drafts)
d["section_label"] = "Essay Seeds"
all_drafts.append(d)
for d in sections["other"]:
d["index"] = len(all_drafts)
d["section_label"] = "Uncategorized"
all_drafts.append(d)
return all_drafts
@mcp.tool()
async def list_vault_drafts() -> list[dict]:
"""Parse Substack Notes Drafts.md from the vault and show all drafts with index numbers.
Returns drafts organized by section (Ready to Post, Essay Seeds, Uncategorized)
with index numbers for use with publish_vault_draft.
"""
drafts = _parse_vault_drafts()
return [
{
"index": d["index"],
"section": d["section_label"],
"title": d["title"][:100],
"preview": d["text"][:200] + ("..." if len(d["text"]) > 200 else ""),
"source": d.get("source"),
"char_count": len(d["text"]),
}
for d in drafts
]
@mcp.tool()
async def publish_vault_draft(
index: int,
publication: str | None = None,
move_to_published: bool = True,
) -> dict:
"""Publish a draft from the vault file as a Substack Note.
Args:
index: Draft index from list_vault_drafts
publication: Publication name from config
move_to_published: If true, move the draft to a Published section in the vault file
"""
drafts = _parse_vault_drafts()
if index < 0 or index >= len(drafts):
return {"error": f"Invalid index {index}. Use list_vault_drafts to see available drafts."}
draft = drafts[index]
text = draft["text"]
# Strip wikilinks for Substack (convert [[X|Y]] to Y, [[X]] to X)
text = re.sub(r"\[\[([^\]|]+)\|([^\]]+)\]\]", r"\2", text)
text = re.sub(r"\[\[([^\]]+)\]\]", r"\1", text)
# Publish
result = await publish_note(text=text, publication=publication)
if isinstance(result, dict) and result.get("status") == "published" and move_to_published:
_move_draft_to_published(draft, result)
return result
def _move_draft_to_published(draft: dict, publish_result: dict):
"""Move a draft from its current section to Published in the vault file."""
cfg = _load_config()
drafts_path = Path(cfg.get("vault_drafts_path", "")).expanduser()
if not drafts_path.exists():
return
content = drafts_path.read_text(encoding="utf-8")
draft_text = draft["text"]
# Remove the draft from its current location
content = content.replace(draft_text, "")
# Clean up double separators
content = re.sub(r"\n---\n\s*\n---\n", "\n---\n", content)
# Add to Published section
now = datetime.now().strftime("%Y-%m-%d %H:%M")
url = publish_result.get("url", "")
published_entry = f"\n\n---\n\n{draft_text}\n\n*Published: {now} | {url}*"
if "## Published" in content:
content = content.replace("## Published", f"## Published{published_entry}", 1)
else:
content += f"\n\n## Published\n{published_entry}"
drafts_path.write_text(content, encoding="utf-8")
@mcp.tool()
async def batch_publish_vault_drafts(
indices: list[int],
publication: str | None = None,
) -> list[dict]:
"""Publish multiple vault drafts as Notes immediately.
Args:
indices: List of draft indices from list_vault_drafts
publication: Publication name from config
"""
results = []
for idx in indices:
result = await publish_vault_draft(index=idx, publication=publication)
results.append({"index": idx, **result})
return results
# ===========================================================================
# PHASE 2: Post Management + Engagement
# ===========================================================================
@mcp.tool()
async def create_draft(
title: str,
body: str,
subtitle: str = "",
audience: str = "everyone",
publication: str | None = None,
section_id: int | None = None,
) -> dict:
"""Create a post draft. Body accepts markdown (auto-converted to Substack format).
Args:
title: Post title
body: Post body in markdown
subtitle: Optional subtitle
audience: "everyone", "only_paid", "founding", "only_free"
publication: Publication name from config
section_id: Optional Substack section ID. Required to publish on
publications that have sections configured. Look up the right
ID via list_sections(publication).
"""
pub = _get_pub(publication)
pm_body = md_to_prosemirror(body)
profile = await _get(f"{GLOBAL_BASE}/user/profile/self", pub)
if isinstance(profile, dict) and "error" in profile:
return profile
user_id = profile.get("id") if isinstance(profile, dict) else None
if not user_id:
return {"error": "Could not fetch user_id for draft_bylines. Check your session cookie."}
payload = {
"draft_title": title,
"draft_subtitle": subtitle or None,
"draft_body": json.dumps(pm_body),
"draft_bylines": [{"id": user_id, "is_guest": False}],
"audience": audience,
"should_send_email": False,
"section_chosen": False,
}
result = await _post(f"{_pub_base(pub)}/drafts", pub, payload)
if isinstance(result, dict) and "error" in result:
return result
draft_id = result.get("id")
# Substack's POST /drafts silently ignores section_id / draft_section_id
# in the create payload. Setting the section requires a follow-up PUT.
if section_id is not None and draft_id is not None:
patch = await _put(
f"{_pub_base(pub)}/drafts/{draft_id}",
pub,
{"draft_section_id": section_id, "section_chosen": True},
)
if isinstance(patch, dict) and "error" in patch:
return patch
return {
"status": "draft_created",
"draft_id": draft_id,
"title": title,
"edit_url": f"https://{pub['subdomain']}.substack.com/publish/post/{draft_id}",
}
@mcp.tool()
async def update_draft(
draft_id: int,
title: str | None = None,
body: str | None = None,
subtitle: str | None = None,
audience: str = "everyone",
publication: str | None = None,
section_id: int | None = None,
) -> dict:
"""Update an existing draft.
Args:
draft_id: The draft ID to update
title: New title (optional)
body: New body in markdown (optional)
subtitle: New subtitle (optional)
audience: "everyone", "only_paid", "founding", "only_free"
publication: Publication name from config
section_id: Optional Substack section ID. Required to publish on
publications that have sections configured. Look up the right
ID via list_sections(publication).
"""
pub = _get_pub(publication)
payload = {}
if title is not None:
payload["draft_title"] = title
if subtitle is not None:
payload["draft_subtitle"] = subtitle
if body is not None:
payload["draft_body"] = json.dumps(md_to_prosemirror(body))
if audience:
payload["audience"] = audience
if section_id is not None:
payload["draft_section_id"] = section_id
payload["section_chosen"] = True
result = await _put(f"{_pub_base(pub)}/drafts/{draft_id}", pub, payload)
if isinstance(result, dict) and "error" in result:
return result
return {"status": "updated", "draft_id": draft_id}
@mcp.tool()
async def publish_post(
draft_id: int,
send_email: bool = True,
audience: str = "everyone",
publication: str | None = None,
section_id: int | None = None,
) -> dict:
"""Publish a draft post live to subscribers.
Args:
draft_id: The draft ID to publish
send_email: Whether to email subscribers (default true)
audience: "everyone", "only_paid", "founding", "only_free"
publication: Publication name from config
section_id: Optional Substack section ID. Required on publications
that have sections configured (otherwise the API returns
"Please choose a section"). When set, the draft is patched to
point at this section before publishing. Look up the right ID
via list_sections(publication).
"""
pub = _get_pub(publication)
if section_id is not None:
patch = await _put(
f"{_pub_base(pub)}/drafts/{draft_id}",
pub,
{"draft_section_id": section_id, "section_chosen": True},
)
if isinstance(patch, dict) and "error" in patch:
return patch
payload = {
"send": send_email,
"share_automatically": False,
"audience": audience,
}
result = await _post(f"{_pub_base(pub)}/drafts/{draft_id}/publish", pub, payload)
if isinstance(result, dict) and "error" in result:
return result
return {
"status": "published",
"post_id": result.get("id"),
"slug": result.get("slug"),
"url": f"https://{pub['subdomain']}.substack.com/p/{result.get('slug', '')}",
}
@mcp.tool()
async def schedule_post(
draft_id: int,
publish_at: str,
publication: str | None = None,
section_id: int | None = None,
) -> dict:
"""Schedule a draft for future publication.
Args:
draft_id: The draft ID to schedule
publish_at: ISO 8601 datetime (e.g., "2026-04-20T14:00:00.000Z")
publication: Publication name from config
section_id: Optional Substack section ID. Required on publications
that have sections configured. When set, the draft is patched
to point at this section before scheduling. Look up the right
ID via list_sections(publication).
"""
pub = _get_pub(publication)
if section_id is not None:
patch = await _put(
f"{_pub_base(pub)}/drafts/{draft_id}",
pub,
{"draft_section_id": section_id, "section_chosen": True},
)
if isinstance(patch, dict) and "error" in patch:
return patch
payload = {"post_date": publish_at}
result = await _post(f"{_pub_base(pub)}/drafts/{draft_id}/schedule", pub, payload)
if isinstance(result, dict) and "error" in result:
return result
return {"status": "scheduled", "draft_id": draft_id, "publish_at": publish_at}
@mcp.tool()
async def list_drafts(
limit: int = 25,
publication: str | None = None,
) -> list[dict]:
"""Show unpublished drafts.
Args:
limit: Max drafts to return
publication: Publication name from config
"""
pub = _get_pub(publication)
result = await _get(
f"{_pub_base(pub)}/drafts",
pub,
params={"offset": "0", "limit": str(limit)},
)
if isinstance(result, dict) and "error" in result:
return [result]
if isinstance(result, list):
drafts = result
else:
drafts = result.get("drafts", result.get("items", []))
return [
{
"id": d.get("id"),
"title": d.get("draft_title", d.get("title", "Untitled")),
"subtitle": d.get("draft_subtitle"),
"created": d.get("draft_created_at", d.get("created_at")),
"word_count": d.get("word_count", 0),
}
for d in drafts[:limit]
]
@mcp.tool()
async def list_published(
limit: int = 25,
publication: str | None = None,
) -> list[dict]:
"""Show published posts with basic stats.
Args:
limit: Max posts to return
publication: Publication name from config
"""
pub = _get_pub(publication)
# Try the post_management endpoint first, fall back to /posts
try:
result = await _get(
f"{_pub_base(pub)}/post_management/published",
pub,
params={
"offset": "0",
"limit": str(limit),
"order_by": "post_date",
"order_direction": "desc",
},
)
except Exception:
# Fallback: use the posts endpoint
result = await _get(
f"{_pub_base(pub)}/posts",
pub,
params={"offset": "0", "limit": str(limit)},
)
if isinstance(result, dict) and "error" in result:
return [result]
posts = result if isinstance(result, list) else result.get("posts", result.get("items", []))
return [
{
"id": p.get("id"),
"title": p.get("title", "Untitled"),
"slug": p.get("slug"),
"date": p.get("post_date"),
"audience": p.get("audience"),
"reactions": p.get("reaction_count", 0),
"comments": p.get("comment_count", 0),
"url": f"https://{pub['subdomain']}.substack.com/p/{p.get('slug', '')}",
}
for p in posts[:limit]
]
@mcp.tool()
async def get_post(
identifier: str,
publication: str | None = None,
) -> dict:
"""Get full post content by slug or numeric ID.
Args:
identifier: Post slug (e.g. "my-post-title") or numeric ID
publication: Publication name from config
"""
pub = _get_pub(publication)
if identifier.isdigit():
result = await _get(f"{GLOBAL_BASE}/posts/by-id/{identifier}", pub)
else:
result = await _get(f"{_pub_base(pub)}/posts/{identifier}", pub)
if isinstance(result, dict) and "error" in result:
return result
return {
"id": result.get("id"),
"title": result.get("title"),
"subtitle": result.get("subtitle"),
"slug": result.get("slug"),
"date": result.get("post_date"),
"audience": result.get("audience"),
"body_html": (result.get("body_html", "") or "")[:2000],
"reactions": result.get("reaction_count", 0),
"comments": result.get("comment_count", 0),
}
@mcp.tool()
async def upload_image(
image_path: str,
publication: str | None = None,
) -> dict:
"""Upload an image to Substack's CDN. Returns the CDN URL for use in posts.
Args:
image_path: Local file path to the image
publication: Publication name from config
"""
import base64
pub = _get_pub(publication)
path = Path(image_path).expanduser()
if not path.exists():
return {"error": f"File not found: {image_path}"}
# Detect MIME type
suffix = path.suffix.lower()
mime_map = {".jpg": "jpeg", ".jpeg": "jpeg", ".png": "png", ".gif": "gif", ".webp": "webp"}
mime = mime_map.get(suffix, "jpeg")
img_data = base64.b64encode(path.read_bytes()).decode("utf-8")
data_uri = f"data:image/{mime};base64,{img_data}"
result = await _post_form(
f"{_pub_base(pub)}/image",
pub,
data={"image": data_uri},
)
if isinstance(result, dict) and "error" in result:
return result
return {"status": "uploaded", "url": result.get("url", "")}
@mcp.tool()
async def react(
post_id: int,
publication: str | None = None,
) -> dict:
"""Heart/like a post.
Args:
post_id: The post ID to react to
publication: Publication name from config
"""
pub = _get_pub(publication)
result = await _post(f"{_pub_base(pub)}/post/{post_id}/reaction", pub, {"reaction": "❤"})
if isinstance(result, dict) and "error" in result:
return result
return {"status": "reacted", "post_id": post_id}
@mcp.tool()
async def restack(
post_id: int,
publication: str | None = None,
) -> dict:
"""Restack a post.
Args:
post_id: The post ID to restack