python-substack 0.5.0__py3-none-any.whl → 0.7.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {python_substack-0.5.0.dist-info → python_substack-0.7.0.dist-info}/METADATA +12 -2
- python_substack-0.7.0.dist-info/RECORD +14 -0
- substack/__init__.py +1 -1
- substack/api.py +111 -0
- substack/cli.py +98 -0
- substack/mdexport.py +381 -0
- substack_mcp/mcp_server.py +386 -386
- python_substack-0.5.0.dist-info/RECORD +0 -13
- {python_substack-0.5.0.dist-info → python_substack-0.7.0.dist-info}/WHEEL +0 -0
- {python_substack-0.5.0.dist-info → python_substack-0.7.0.dist-info}/entry_points.txt +0 -0
- {python_substack-0.5.0.dist-info → python_substack-0.7.0.dist-info}/licenses/LICENSE +0 -0
substack_mcp/mcp_server.py
CHANGED
|
@@ -1,386 +1,386 @@
|
|
|
1
|
-
from __future__ import annotations
|
|
2
|
-
|
|
3
|
-
import os
|
|
4
|
-
from typing import Any, Dict, List, Optional
|
|
5
|
-
|
|
6
|
-
try:
|
|
7
|
-
from dotenv import load_dotenv
|
|
8
|
-
except ImportError:
|
|
9
|
-
load_dotenv = None
|
|
10
|
-
|
|
11
|
-
from
|
|
12
|
-
|
|
13
|
-
from substack.api import Api
|
|
14
|
-
from substack.post import Post
|
|
15
|
-
|
|
16
|
-
if load_dotenv is not None:
|
|
17
|
-
load_dotenv()
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
def get_api() -> Api:
|
|
21
|
-
email = os.getenv("EMAIL")
|
|
22
|
-
password = os.getenv("PASSWORD")
|
|
23
|
-
cookies_path = os.getenv("COOKIES_PATH")
|
|
24
|
-
cookies_string = os.getenv("COOKIES_STRING")
|
|
25
|
-
publication_url = os.getenv("PUBLICATION_URL")
|
|
26
|
-
|
|
27
|
-
if cookies_path or cookies_string:
|
|
28
|
-
return Api(
|
|
29
|
-
cookies_path=cookies_path,
|
|
30
|
-
cookies_string=cookies_string,
|
|
31
|
-
publication_url=publication_url,
|
|
32
|
-
)
|
|
33
|
-
|
|
34
|
-
if email and password:
|
|
35
|
-
return Api(
|
|
36
|
-
email=email,
|
|
37
|
-
password=password,
|
|
38
|
-
publication_url=publication_url,
|
|
39
|
-
)
|
|
40
|
-
|
|
41
|
-
raise ValueError(
|
|
42
|
-
"Missing Substack auth configuration: set EMAIL/PASSWORD or COOKIES_PATH/COOKIES_STRING"
|
|
43
|
-
)
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
def _normalize_tags(tags: Optional[Any]) -> List[str]:
|
|
47
|
-
if tags is None:
|
|
48
|
-
return []
|
|
49
|
-
if isinstance(tags, str):
|
|
50
|
-
return [tags]
|
|
51
|
-
if isinstance(tags, list):
|
|
52
|
-
return [str(tag) for tag in tags]
|
|
53
|
-
raise ValueError("tags must be a string or a list of strings")
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
mcp = FastMCP("substack")
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
@mcp.tool()
|
|
60
|
-
async def post_draft_from_markdown(
|
|
61
|
-
title: str,
|
|
62
|
-
markdown: str,
|
|
63
|
-
subtitle: Optional[str] = "",
|
|
64
|
-
audience: str = "everyone",
|
|
65
|
-
write_comment_permissions: str = "everyone",
|
|
66
|
-
search_engine_title: Optional[str] = None,
|
|
67
|
-
search_engine_description: Optional[str] = None,
|
|
68
|
-
slug: Optional[str] = None,
|
|
69
|
-
draft_section_id: Optional[int] = None,
|
|
70
|
-
tags: Optional[Any] = None,
|
|
71
|
-
prepublish: bool = False,
|
|
72
|
-
publish: bool = False,
|
|
73
|
-
send: bool = True,
|
|
74
|
-
share_automatically: bool = False,
|
|
75
|
-
) -> Dict[str, Any]:
|
|
76
|
-
"""Create or update a Substack draft from Markdown.
|
|
77
|
-
|
|
78
|
-
This tool builds a Substack `Post` from markdown content and posts a draft.
|
|
79
|
-
It supports optional tag assignment, prepublish (setup check), and publishing.
|
|
80
|
-
|
|
81
|
-
Args:
|
|
82
|
-
title: Draft title.
|
|
83
|
-
markdown: Markdown body content.
|
|
84
|
-
subtitle: Optional subtitle text.
|
|
85
|
-
audience: One of `everyone`, `only_paid`, `founding`, `only_free`.
|
|
86
|
-
write_comment_permissions: One of `none`, `only_paid`, `everyone`.
|
|
87
|
-
search_engine_title: Optional title for search engine optimization.
|
|
88
|
-
search_engine_description: Optional description for search engine optimization.
|
|
89
|
-
slug: Optional URL slug for the post.
|
|
90
|
-
draft_section_id: Optional section ID for the draft.
|
|
91
|
-
tags: Tag or list of tags to attach to the post.
|
|
92
|
-
prepublish: If true, calls `prepublish_draft` after creation.
|
|
93
|
-
publish: If true, calls `publish_draft` after creation (and optionally prepublish).
|
|
94
|
-
send: Passed to `publish_draft` for newsletter delivery.
|
|
95
|
-
share_automatically: Passed to `publish_draft`.
|
|
96
|
-
|
|
97
|
-
Returns:
|
|
98
|
-
dict containing drafted post (`draft`), optional `tags`, `prepublish`, `publish` results.
|
|
99
|
-
|
|
100
|
-
Examples:
|
|
101
|
-
With the YAML structure from the README, a caller can map fields like:
|
|
102
|
-
|
|
103
|
-
```yaml
|
|
104
|
-
title: "My Post Title"
|
|
105
|
-
subtitle: "My Post Subtitle"
|
|
106
|
-
audience: "everyone"
|
|
107
|
-
write_comment_permissions: "everyone"
|
|
108
|
-
markdown: |
|
|
109
|
-
# Hello
|
|
110
|
-
|
|
111
|
-
This is the body.
|
|
112
|
-
|
|
113
|
-
tags:
|
|
114
|
-
- python
|
|
115
|
-
- substack
|
|
116
|
-
prepublish: true
|
|
117
|
-
publish: true
|
|
118
|
-
send: false
|
|
119
|
-
share_automatically: true
|
|
120
|
-
```
|
|
121
|
-
|
|
122
|
-
Then invoke via MCP directly:
|
|
123
|
-
|
|
124
|
-
```python
|
|
125
|
-
from substack_mcp.mcp_server import post_draft_from_markdown
|
|
126
|
-
|
|
127
|
-
result = await post_draft_from_markdown(
|
|
128
|
-
title='My Post Title',
|
|
129
|
-
markdown='# Hello\n\nThis is the body.',
|
|
130
|
-
subtitle='My Post Subtitle',
|
|
131
|
-
audience='everyone',
|
|
132
|
-
write_comment_permissions='everyone',
|
|
133
|
-
tags=['python', 'substack'],
|
|
134
|
-
prepublish=True,
|
|
135
|
-
publish=False, # set true when ready
|
|
136
|
-
)
|
|
137
|
-
print(result)
|
|
138
|
-
```
|
|
139
|
-
|
|
140
|
-
A longer process with manual prepublish/publish calls:
|
|
141
|
-
|
|
142
|
-
```python
|
|
143
|
-
from substack_mcp.mcp_server import (
|
|
144
|
-
post_draft_from_markdown,
|
|
145
|
-
prepublish_draft,
|
|
146
|
-
publish_draft,
|
|
147
|
-
add_tags,
|
|
148
|
-
)
|
|
149
|
-
|
|
150
|
-
d = await post_draft_from_markdown(
|
|
151
|
-
title='Long flow',
|
|
152
|
-
markdown='Content',
|
|
153
|
-
tags=['a','b'],
|
|
154
|
-
publish=False,
|
|
155
|
-
)
|
|
156
|
-
draft_id = d['draft']['id']
|
|
157
|
-
|
|
158
|
-
await add_tags(draft_id, ['post-tag', 'news'])
|
|
159
|
-
await prepublish_draft(draft_id)
|
|
160
|
-
await publish_draft(draft_id, send=True, share_automatically=True)
|
|
161
|
-
```
|
|
162
|
-
|
|
163
|
-
This docstring example is meant to mirror the YAML-driven workflow and show how to decompose the same operations into explicit tool calls.
|
|
164
|
-
"""
|
|
165
|
-
client = get_api()
|
|
166
|
-
|
|
167
|
-
return client.create_draft_from_markdown(
|
|
168
|
-
title=title,
|
|
169
|
-
markdown=markdown,
|
|
170
|
-
subtitle=subtitle,
|
|
171
|
-
audience=audience,
|
|
172
|
-
write_comment_permissions=write_comment_permissions,
|
|
173
|
-
search_engine_title=search_engine_title,
|
|
174
|
-
search_engine_description=search_engine_description,
|
|
175
|
-
slug=slug,
|
|
176
|
-
draft_section_id=draft_section_id,
|
|
177
|
-
tags=tags,
|
|
178
|
-
prepublish=prepublish,
|
|
179
|
-
publish=publish,
|
|
180
|
-
send=send,
|
|
181
|
-
share_automatically=share_automatically,
|
|
182
|
-
)
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
@mcp.tool()
|
|
186
|
-
async def put_draft(
|
|
187
|
-
draft_id: int,
|
|
188
|
-
update_payload: Dict[str, Any],
|
|
189
|
-
) -> Dict[str, Any]:
|
|
190
|
-
"""Update an existing draft by draft ID.
|
|
191
|
-
|
|
192
|
-
Args:
|
|
193
|
-
draft_id: target draft identifier.
|
|
194
|
-
update_payload: dict of fields supported by Substack `put_draft` (e.g. `slug`, `draft_section_id`).
|
|
195
|
-
|
|
196
|
-
Returns:
|
|
197
|
-
API response dict for the updated draft.
|
|
198
|
-
"""
|
|
199
|
-
client = get_api()
|
|
200
|
-
return client.put_draft(draft_id, **update_payload)
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
@mcp.tool()
|
|
204
|
-
async def add_tags(draft_id: int, tags: Any) -> Dict[str, Any]:
|
|
205
|
-
"""Add tags to a specific draft/post.
|
|
206
|
-
|
|
207
|
-
Args:
|
|
208
|
-
draft_id: target draft identifier.
|
|
209
|
-
tags: string or list of tag names (e.g. `"tech"` or `["tech", "python"]`).
|
|
210
|
-
|
|
211
|
-
Returns:
|
|
212
|
-
Response from `add_tags_to_post` (tag IDs + names).
|
|
213
|
-
"""
|
|
214
|
-
client = get_api()
|
|
215
|
-
tags_list = _normalize_tags(tags)
|
|
216
|
-
if not tags_list:
|
|
217
|
-
raise ValueError("tags is required and cannot be empty")
|
|
218
|
-
return client.add_tags_to_post(draft_id, tags_list)
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
@mcp.tool()
|
|
222
|
-
async def prepublish_draft(draft_id: int) -> Dict[str, Any]:
|
|
223
|
-
"""Invoke prepublish checks for a draft.
|
|
224
|
-
|
|
225
|
-
Args:
|
|
226
|
-
draft_id: target draft identifier.
|
|
227
|
-
|
|
228
|
-
Returns:
|
|
229
|
-
Prepublish response dict from Substack API.
|
|
230
|
-
"""
|
|
231
|
-
client = get_api()
|
|
232
|
-
return client.prepublish_draft(draft_id)
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
@mcp.tool()
|
|
236
|
-
async def publish_draft(
|
|
237
|
-
draft_id: int,
|
|
238
|
-
send: bool = True,
|
|
239
|
-
share_automatically: bool = False,
|
|
240
|
-
) -> Dict[str, Any]:
|
|
241
|
-
"""Publish a draft to live post state. (Legacy compatibility interface).
|
|
242
|
-
|
|
243
|
-
This tool remains for backward compatibility. It is recommended to use
|
|
244
|
-
`publish_draft_checked` instead, which provides a safer publishing path
|
|
245
|
-
with explicit confirmation and prepublish validation.
|
|
246
|
-
|
|
247
|
-
Args:
|
|
248
|
-
draft_id: target draft identifier.
|
|
249
|
-
send: if False then do not send email to subscribers.
|
|
250
|
-
share_automatically: whether to auto-share (e.g. social propagation).
|
|
251
|
-
|
|
252
|
-
Returns:
|
|
253
|
-
Response from Substack `publish_draft`.
|
|
254
|
-
"""
|
|
255
|
-
client = get_api()
|
|
256
|
-
return client.publish_draft(
|
|
257
|
-
draft_id, send=send, share_automatically=share_automatically
|
|
258
|
-
)
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
@mcp.tool()
|
|
262
|
-
async def publish_draft_checked(
|
|
263
|
-
draft_id: int,
|
|
264
|
-
confirm: bool = False,
|
|
265
|
-
send: bool = False,
|
|
266
|
-
share_automatically: bool = False,
|
|
267
|
-
) -> Dict[str, Any]:
|
|
268
|
-
"""A safer publishing path that requires confirmation and runs prepublish checks.
|
|
269
|
-
|
|
270
|
-
Args:
|
|
271
|
-
draft_id: target draft identifier.
|
|
272
|
-
confirm: Must be True to proceed with publication.
|
|
273
|
-
send: if False then do not send email to subscribers. Defaults to False.
|
|
274
|
-
share_automatically: whether to auto-share.
|
|
275
|
-
|
|
276
|
-
Returns:
|
|
277
|
-
Response from Substack `publish_draft`.
|
|
278
|
-
"""
|
|
279
|
-
if not confirm:
|
|
280
|
-
raise ValueError("Publishing rejected: confirm parameter must be True.")
|
|
281
|
-
|
|
282
|
-
client = get_api()
|
|
283
|
-
client.prepublish_draft(draft_id)
|
|
284
|
-
return client.publish_draft(
|
|
285
|
-
draft_id, send=send, share_automatically=share_automatically
|
|
286
|
-
)
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
@mcp.tool()
|
|
290
|
-
async def get_status() -> Dict[str, Any]:
|
|
291
|
-
"""Get the authentication status and basic user information.
|
|
292
|
-
|
|
293
|
-
Returns:
|
|
294
|
-
A dictionary containing user profile and primary publication details.
|
|
295
|
-
"""
|
|
296
|
-
client = get_api()
|
|
297
|
-
profile = client.get_user_profile()
|
|
298
|
-
primary_pub = client.get_user_primary_publication()
|
|
299
|
-
return {"profile": profile, "primary_publication": primary_pub}
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
@mcp.tool()
|
|
303
|
-
async def list_publications() -> List[Dict[str, Any]]:
|
|
304
|
-
"""List all publications available to the authenticated user.
|
|
305
|
-
|
|
306
|
-
Returns:
|
|
307
|
-
A list of publications.
|
|
308
|
-
"""
|
|
309
|
-
client = get_api()
|
|
310
|
-
return client.get_user_publications()
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
@mcp.tool()
|
|
314
|
-
async def list_drafts(
|
|
315
|
-
filter: str = "draft", offset: int = 0, limit: int = 25
|
|
316
|
-
) -> List[Dict[str, Any]]:
|
|
317
|
-
"""List drafts for the current publication.
|
|
318
|
-
|
|
319
|
-
Args:
|
|
320
|
-
filter: Filter string, defaults to "draft".
|
|
321
|
-
offset: Pagination offset.
|
|
322
|
-
limit: Max number of drafts to return.
|
|
323
|
-
|
|
324
|
-
Returns:
|
|
325
|
-
A list of drafts.
|
|
326
|
-
"""
|
|
327
|
-
client = get_api()
|
|
328
|
-
return client.get_drafts(filter=filter, offset=offset, limit=limit)
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
@mcp.tool()
|
|
332
|
-
async def get_draft(draft_id: int) -> Dict[str, Any]:
|
|
333
|
-
"""Get a specific draft by its ID.
|
|
334
|
-
|
|
335
|
-
Args:
|
|
336
|
-
draft_id: The identifier of the draft.
|
|
337
|
-
|
|
338
|
-
Returns:
|
|
339
|
-
The draft details.
|
|
340
|
-
"""
|
|
341
|
-
client = get_api()
|
|
342
|
-
return client.get_draft(draft_id)
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
@mcp.tool()
|
|
346
|
-
async def schedule_draft(draft_id: int, at: str) -> Dict[str, Any]:
|
|
347
|
-
"""Schedule a draft for release.
|
|
348
|
-
|
|
349
|
-
Args:
|
|
350
|
-
draft_id: target draft identifier.
|
|
351
|
-
at: ISO 8601 formatted datetime string (e.g., "2024-01-01T12:00:00Z").
|
|
352
|
-
|
|
353
|
-
Returns:
|
|
354
|
-
API response dict for the scheduled draft.
|
|
355
|
-
"""
|
|
356
|
-
from datetime import datetime
|
|
357
|
-
|
|
358
|
-
try:
|
|
359
|
-
draft_datetime = datetime.fromisoformat(at.replace("Z", "+00:00"))
|
|
360
|
-
except ValueError as e:
|
|
361
|
-
raise ValueError(f"Invalid ISO datetime string for 'at': {e}")
|
|
362
|
-
|
|
363
|
-
client = get_api()
|
|
364
|
-
return client.schedule_draft(draft_id, draft_datetime)
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
@mcp.tool()
|
|
368
|
-
async def unschedule_draft(draft_id: int) -> Dict[str, Any]:
|
|
369
|
-
"""Unschedule a previously scheduled draft.
|
|
370
|
-
|
|
371
|
-
Args:
|
|
372
|
-
draft_id: target draft identifier.
|
|
373
|
-
|
|
374
|
-
Returns:
|
|
375
|
-
API response dict for unscheduling.
|
|
376
|
-
"""
|
|
377
|
-
client = get_api()
|
|
378
|
-
return client.unschedule_draft(draft_id)
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
def main() -> None:
|
|
382
|
-
mcp.run(transport="stdio")
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
if __name__ == "__main__":
|
|
386
|
-
main()
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
from typing import Any, Dict, List, Optional
|
|
5
|
+
|
|
6
|
+
try:
|
|
7
|
+
from dotenv import load_dotenv
|
|
8
|
+
except ImportError:
|
|
9
|
+
load_dotenv = None
|
|
10
|
+
|
|
11
|
+
from fastmcp import FastMCP
|
|
12
|
+
|
|
13
|
+
from substack.api import Api
|
|
14
|
+
from substack.post import Post
|
|
15
|
+
|
|
16
|
+
if load_dotenv is not None:
|
|
17
|
+
load_dotenv()
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def get_api() -> Api:
|
|
21
|
+
email = os.getenv("EMAIL")
|
|
22
|
+
password = os.getenv("PASSWORD")
|
|
23
|
+
cookies_path = os.getenv("COOKIES_PATH")
|
|
24
|
+
cookies_string = os.getenv("COOKIES_STRING")
|
|
25
|
+
publication_url = os.getenv("PUBLICATION_URL")
|
|
26
|
+
|
|
27
|
+
if cookies_path or cookies_string:
|
|
28
|
+
return Api(
|
|
29
|
+
cookies_path=cookies_path,
|
|
30
|
+
cookies_string=cookies_string,
|
|
31
|
+
publication_url=publication_url,
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
if email and password:
|
|
35
|
+
return Api(
|
|
36
|
+
email=email,
|
|
37
|
+
password=password,
|
|
38
|
+
publication_url=publication_url,
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
raise ValueError(
|
|
42
|
+
"Missing Substack auth configuration: set EMAIL/PASSWORD or COOKIES_PATH/COOKIES_STRING"
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def _normalize_tags(tags: Optional[Any]) -> List[str]:
|
|
47
|
+
if tags is None:
|
|
48
|
+
return []
|
|
49
|
+
if isinstance(tags, str):
|
|
50
|
+
return [tags]
|
|
51
|
+
if isinstance(tags, list):
|
|
52
|
+
return [str(tag) for tag in tags]
|
|
53
|
+
raise ValueError("tags must be a string or a list of strings")
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
mcp = FastMCP("substack")
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
@mcp.tool()
|
|
60
|
+
async def post_draft_from_markdown(
|
|
61
|
+
title: str,
|
|
62
|
+
markdown: str,
|
|
63
|
+
subtitle: Optional[str] = "",
|
|
64
|
+
audience: str = "everyone",
|
|
65
|
+
write_comment_permissions: str = "everyone",
|
|
66
|
+
search_engine_title: Optional[str] = None,
|
|
67
|
+
search_engine_description: Optional[str] = None,
|
|
68
|
+
slug: Optional[str] = None,
|
|
69
|
+
draft_section_id: Optional[int] = None,
|
|
70
|
+
tags: Optional[Any] = None,
|
|
71
|
+
prepublish: bool = False,
|
|
72
|
+
publish: bool = False,
|
|
73
|
+
send: bool = True,
|
|
74
|
+
share_automatically: bool = False,
|
|
75
|
+
) -> Dict[str, Any]:
|
|
76
|
+
"""Create or update a Substack draft from Markdown.
|
|
77
|
+
|
|
78
|
+
This tool builds a Substack `Post` from markdown content and posts a draft.
|
|
79
|
+
It supports optional tag assignment, prepublish (setup check), and publishing.
|
|
80
|
+
|
|
81
|
+
Args:
|
|
82
|
+
title: Draft title.
|
|
83
|
+
markdown: Markdown body content.
|
|
84
|
+
subtitle: Optional subtitle text.
|
|
85
|
+
audience: One of `everyone`, `only_paid`, `founding`, `only_free`.
|
|
86
|
+
write_comment_permissions: One of `none`, `only_paid`, `everyone`.
|
|
87
|
+
search_engine_title: Optional title for search engine optimization.
|
|
88
|
+
search_engine_description: Optional description for search engine optimization.
|
|
89
|
+
slug: Optional URL slug for the post.
|
|
90
|
+
draft_section_id: Optional section ID for the draft.
|
|
91
|
+
tags: Tag or list of tags to attach to the post.
|
|
92
|
+
prepublish: If true, calls `prepublish_draft` after creation.
|
|
93
|
+
publish: If true, calls `publish_draft` after creation (and optionally prepublish).
|
|
94
|
+
send: Passed to `publish_draft` for newsletter delivery.
|
|
95
|
+
share_automatically: Passed to `publish_draft`.
|
|
96
|
+
|
|
97
|
+
Returns:
|
|
98
|
+
dict containing drafted post (`draft`), optional `tags`, `prepublish`, `publish` results.
|
|
99
|
+
|
|
100
|
+
Examples:
|
|
101
|
+
With the YAML structure from the README, a caller can map fields like:
|
|
102
|
+
|
|
103
|
+
```yaml
|
|
104
|
+
title: "My Post Title"
|
|
105
|
+
subtitle: "My Post Subtitle"
|
|
106
|
+
audience: "everyone"
|
|
107
|
+
write_comment_permissions: "everyone"
|
|
108
|
+
markdown: |
|
|
109
|
+
# Hello
|
|
110
|
+
|
|
111
|
+
This is the body.
|
|
112
|
+
|
|
113
|
+
tags:
|
|
114
|
+
- python
|
|
115
|
+
- substack
|
|
116
|
+
prepublish: true
|
|
117
|
+
publish: true
|
|
118
|
+
send: false
|
|
119
|
+
share_automatically: true
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Then invoke via MCP directly:
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
from substack_mcp.mcp_server import post_draft_from_markdown
|
|
126
|
+
|
|
127
|
+
result = await post_draft_from_markdown(
|
|
128
|
+
title='My Post Title',
|
|
129
|
+
markdown='# Hello\n\nThis is the body.',
|
|
130
|
+
subtitle='My Post Subtitle',
|
|
131
|
+
audience='everyone',
|
|
132
|
+
write_comment_permissions='everyone',
|
|
133
|
+
tags=['python', 'substack'],
|
|
134
|
+
prepublish=True,
|
|
135
|
+
publish=False, # set true when ready
|
|
136
|
+
)
|
|
137
|
+
print(result)
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
A longer process with manual prepublish/publish calls:
|
|
141
|
+
|
|
142
|
+
```python
|
|
143
|
+
from substack_mcp.mcp_server import (
|
|
144
|
+
post_draft_from_markdown,
|
|
145
|
+
prepublish_draft,
|
|
146
|
+
publish_draft,
|
|
147
|
+
add_tags,
|
|
148
|
+
)
|
|
149
|
+
|
|
150
|
+
d = await post_draft_from_markdown(
|
|
151
|
+
title='Long flow',
|
|
152
|
+
markdown='Content',
|
|
153
|
+
tags=['a','b'],
|
|
154
|
+
publish=False,
|
|
155
|
+
)
|
|
156
|
+
draft_id = d['draft']['id']
|
|
157
|
+
|
|
158
|
+
await add_tags(draft_id, ['post-tag', 'news'])
|
|
159
|
+
await prepublish_draft(draft_id)
|
|
160
|
+
await publish_draft(draft_id, send=True, share_automatically=True)
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
This docstring example is meant to mirror the YAML-driven workflow and show how to decompose the same operations into explicit tool calls.
|
|
164
|
+
"""
|
|
165
|
+
client = get_api()
|
|
166
|
+
|
|
167
|
+
return client.create_draft_from_markdown(
|
|
168
|
+
title=title,
|
|
169
|
+
markdown=markdown,
|
|
170
|
+
subtitle=subtitle,
|
|
171
|
+
audience=audience,
|
|
172
|
+
write_comment_permissions=write_comment_permissions,
|
|
173
|
+
search_engine_title=search_engine_title,
|
|
174
|
+
search_engine_description=search_engine_description,
|
|
175
|
+
slug=slug,
|
|
176
|
+
draft_section_id=draft_section_id,
|
|
177
|
+
tags=tags,
|
|
178
|
+
prepublish=prepublish,
|
|
179
|
+
publish=publish,
|
|
180
|
+
send=send,
|
|
181
|
+
share_automatically=share_automatically,
|
|
182
|
+
)
|
|
183
|
+
|
|
184
|
+
|
|
185
|
+
@mcp.tool()
|
|
186
|
+
async def put_draft(
|
|
187
|
+
draft_id: int,
|
|
188
|
+
update_payload: Dict[str, Any],
|
|
189
|
+
) -> Dict[str, Any]:
|
|
190
|
+
"""Update an existing draft by draft ID.
|
|
191
|
+
|
|
192
|
+
Args:
|
|
193
|
+
draft_id: target draft identifier.
|
|
194
|
+
update_payload: dict of fields supported by Substack `put_draft` (e.g. `slug`, `draft_section_id`).
|
|
195
|
+
|
|
196
|
+
Returns:
|
|
197
|
+
API response dict for the updated draft.
|
|
198
|
+
"""
|
|
199
|
+
client = get_api()
|
|
200
|
+
return client.put_draft(draft_id, **update_payload)
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
@mcp.tool()
|
|
204
|
+
async def add_tags(draft_id: int, tags: Any) -> Dict[str, Any]:
|
|
205
|
+
"""Add tags to a specific draft/post.
|
|
206
|
+
|
|
207
|
+
Args:
|
|
208
|
+
draft_id: target draft identifier.
|
|
209
|
+
tags: string or list of tag names (e.g. `"tech"` or `["tech", "python"]`).
|
|
210
|
+
|
|
211
|
+
Returns:
|
|
212
|
+
Response from `add_tags_to_post` (tag IDs + names).
|
|
213
|
+
"""
|
|
214
|
+
client = get_api()
|
|
215
|
+
tags_list = _normalize_tags(tags)
|
|
216
|
+
if not tags_list:
|
|
217
|
+
raise ValueError("tags is required and cannot be empty")
|
|
218
|
+
return client.add_tags_to_post(draft_id, tags_list)
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
@mcp.tool()
|
|
222
|
+
async def prepublish_draft(draft_id: int) -> Dict[str, Any]:
|
|
223
|
+
"""Invoke prepublish checks for a draft.
|
|
224
|
+
|
|
225
|
+
Args:
|
|
226
|
+
draft_id: target draft identifier.
|
|
227
|
+
|
|
228
|
+
Returns:
|
|
229
|
+
Prepublish response dict from Substack API.
|
|
230
|
+
"""
|
|
231
|
+
client = get_api()
|
|
232
|
+
return client.prepublish_draft(draft_id)
|
|
233
|
+
|
|
234
|
+
|
|
235
|
+
@mcp.tool()
|
|
236
|
+
async def publish_draft(
|
|
237
|
+
draft_id: int,
|
|
238
|
+
send: bool = True,
|
|
239
|
+
share_automatically: bool = False,
|
|
240
|
+
) -> Dict[str, Any]:
|
|
241
|
+
"""Publish a draft to live post state. (Legacy compatibility interface).
|
|
242
|
+
|
|
243
|
+
This tool remains for backward compatibility. It is recommended to use
|
|
244
|
+
`publish_draft_checked` instead, which provides a safer publishing path
|
|
245
|
+
with explicit confirmation and prepublish validation.
|
|
246
|
+
|
|
247
|
+
Args:
|
|
248
|
+
draft_id: target draft identifier.
|
|
249
|
+
send: if False then do not send email to subscribers.
|
|
250
|
+
share_automatically: whether to auto-share (e.g. social propagation).
|
|
251
|
+
|
|
252
|
+
Returns:
|
|
253
|
+
Response from Substack `publish_draft`.
|
|
254
|
+
"""
|
|
255
|
+
client = get_api()
|
|
256
|
+
return client.publish_draft(
|
|
257
|
+
draft_id, send=send, share_automatically=share_automatically
|
|
258
|
+
)
|
|
259
|
+
|
|
260
|
+
|
|
261
|
+
@mcp.tool()
|
|
262
|
+
async def publish_draft_checked(
|
|
263
|
+
draft_id: int,
|
|
264
|
+
confirm: bool = False,
|
|
265
|
+
send: bool = False,
|
|
266
|
+
share_automatically: bool = False,
|
|
267
|
+
) -> Dict[str, Any]:
|
|
268
|
+
"""A safer publishing path that requires confirmation and runs prepublish checks.
|
|
269
|
+
|
|
270
|
+
Args:
|
|
271
|
+
draft_id: target draft identifier.
|
|
272
|
+
confirm: Must be True to proceed with publication.
|
|
273
|
+
send: if False then do not send email to subscribers. Defaults to False.
|
|
274
|
+
share_automatically: whether to auto-share.
|
|
275
|
+
|
|
276
|
+
Returns:
|
|
277
|
+
Response from Substack `publish_draft`.
|
|
278
|
+
"""
|
|
279
|
+
if not confirm:
|
|
280
|
+
raise ValueError("Publishing rejected: confirm parameter must be True.")
|
|
281
|
+
|
|
282
|
+
client = get_api()
|
|
283
|
+
client.prepublish_draft(draft_id)
|
|
284
|
+
return client.publish_draft(
|
|
285
|
+
draft_id, send=send, share_automatically=share_automatically
|
|
286
|
+
)
|
|
287
|
+
|
|
288
|
+
|
|
289
|
+
@mcp.tool()
|
|
290
|
+
async def get_status() -> Dict[str, Any]:
|
|
291
|
+
"""Get the authentication status and basic user information.
|
|
292
|
+
|
|
293
|
+
Returns:
|
|
294
|
+
A dictionary containing user profile and primary publication details.
|
|
295
|
+
"""
|
|
296
|
+
client = get_api()
|
|
297
|
+
profile = client.get_user_profile()
|
|
298
|
+
primary_pub = client.get_user_primary_publication()
|
|
299
|
+
return {"profile": profile, "primary_publication": primary_pub}
|
|
300
|
+
|
|
301
|
+
|
|
302
|
+
@mcp.tool()
|
|
303
|
+
async def list_publications() -> List[Dict[str, Any]]:
|
|
304
|
+
"""List all publications available to the authenticated user.
|
|
305
|
+
|
|
306
|
+
Returns:
|
|
307
|
+
A list of publications.
|
|
308
|
+
"""
|
|
309
|
+
client = get_api()
|
|
310
|
+
return client.get_user_publications()
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
@mcp.tool()
|
|
314
|
+
async def list_drafts(
|
|
315
|
+
filter: str = "draft", offset: int = 0, limit: int = 25
|
|
316
|
+
) -> List[Dict[str, Any]]:
|
|
317
|
+
"""List drafts for the current publication.
|
|
318
|
+
|
|
319
|
+
Args:
|
|
320
|
+
filter: Filter string, defaults to "draft".
|
|
321
|
+
offset: Pagination offset.
|
|
322
|
+
limit: Max number of drafts to return.
|
|
323
|
+
|
|
324
|
+
Returns:
|
|
325
|
+
A list of drafts.
|
|
326
|
+
"""
|
|
327
|
+
client = get_api()
|
|
328
|
+
return client.get_drafts(filter=filter, offset=offset, limit=limit)
|
|
329
|
+
|
|
330
|
+
|
|
331
|
+
@mcp.tool()
|
|
332
|
+
async def get_draft(draft_id: int) -> Dict[str, Any]:
|
|
333
|
+
"""Get a specific draft by its ID.
|
|
334
|
+
|
|
335
|
+
Args:
|
|
336
|
+
draft_id: The identifier of the draft.
|
|
337
|
+
|
|
338
|
+
Returns:
|
|
339
|
+
The draft details.
|
|
340
|
+
"""
|
|
341
|
+
client = get_api()
|
|
342
|
+
return client.get_draft(draft_id)
|
|
343
|
+
|
|
344
|
+
|
|
345
|
+
@mcp.tool()
|
|
346
|
+
async def schedule_draft(draft_id: int, at: str) -> Dict[str, Any]:
|
|
347
|
+
"""Schedule a draft for release.
|
|
348
|
+
|
|
349
|
+
Args:
|
|
350
|
+
draft_id: target draft identifier.
|
|
351
|
+
at: ISO 8601 formatted datetime string (e.g., "2024-01-01T12:00:00Z").
|
|
352
|
+
|
|
353
|
+
Returns:
|
|
354
|
+
API response dict for the scheduled draft.
|
|
355
|
+
"""
|
|
356
|
+
from datetime import datetime
|
|
357
|
+
|
|
358
|
+
try:
|
|
359
|
+
draft_datetime = datetime.fromisoformat(at.replace("Z", "+00:00"))
|
|
360
|
+
except ValueError as e:
|
|
361
|
+
raise ValueError(f"Invalid ISO datetime string for 'at': {e}")
|
|
362
|
+
|
|
363
|
+
client = get_api()
|
|
364
|
+
return client.schedule_draft(draft_id, draft_datetime)
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
@mcp.tool()
|
|
368
|
+
async def unschedule_draft(draft_id: int) -> Dict[str, Any]:
|
|
369
|
+
"""Unschedule a previously scheduled draft.
|
|
370
|
+
|
|
371
|
+
Args:
|
|
372
|
+
draft_id: target draft identifier.
|
|
373
|
+
|
|
374
|
+
Returns:
|
|
375
|
+
API response dict for unscheduling.
|
|
376
|
+
"""
|
|
377
|
+
client = get_api()
|
|
378
|
+
return client.unschedule_draft(draft_id)
|
|
379
|
+
|
|
380
|
+
|
|
381
|
+
def main() -> None:
|
|
382
|
+
mcp.run(transport="stdio")
|
|
383
|
+
|
|
384
|
+
|
|
385
|
+
if __name__ == "__main__":
|
|
386
|
+
main()
|