Skip to content

Commit 2e16872

Browse files
committed
Add debug logging for list_drafts issue
1 parent fc5dcb3 commit 2e16872

File tree

3 files changed

+51
-15
lines changed

3 files changed

+51
-15
lines changed

src/handlers/post_handler.py

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,36 @@ async def list_drafts(self, limit: int = 10) -> List[Dict[str, Any]]:
240240
# python-substack returns a generator, convert to list
241241
# The API returns all posts, so we need to filter for drafts only
242242
try:
243-
all_posts = list(self.client.get_drafts(limit=min(limit * 3, 50))) # Get more to ensure we have enough drafts, but cap at 50
244-
logger.debug(f"Retrieved {len(all_posts)} posts from API")
243+
# Debug: Let's see what client we're using
244+
logger.info(f"Client type: {type(self.client)}")
245+
logger.info(f"Client class name: {self.client.__class__.__name__}")
246+
247+
# Check publication info
248+
if hasattr(self.client, 'client') and hasattr(self.client.client, 'publication_id'):
249+
logger.info(f"Publication ID: {self.client.client.publication_id}")
250+
if hasattr(self.client, 'client') and hasattr(self.client.client, 'subdomain'):
251+
logger.info(f"Subdomain: {self.client.client.subdomain}")
252+
253+
# Try to get the raw API response
254+
raw_result = self.client.get_drafts(limit=min(limit * 3, 50))
255+
logger.info(f"Raw result type: {type(raw_result)}")
256+
257+
all_posts = list(raw_result) # Get more to ensure we have enough drafts, but cap at 50
258+
logger.info(f"Retrieved {len(all_posts)} posts from API")
259+
260+
# If empty, let's try a different approach
261+
if len(all_posts) == 0:
262+
logger.warning("get_drafts returned empty, trying direct API call")
263+
# Check if we have the underlying client
264+
if hasattr(self.client, 'client'):
265+
logger.info("Found underlying client, checking its properties")
266+
inner_client = self.client.client
267+
logger.info(f"Inner client type: {type(inner_client)}")
268+
logger.info(f"Inner client dir: {[x for x in dir(inner_client) if not x.startswith('_')][:10]}")
245269
except Exception as e:
246-
logger.error(f"Error getting drafts: {e}")
270+
logger.error(f"Error getting drafts: {type(e).__name__}: {e}")
271+
import traceback
272+
logger.error(traceback.format_exc())
247273
return []
248274

249275
drafts = []
@@ -256,17 +282,10 @@ async def list_drafts(self, limit: int = 10) -> List[Dict[str, Any]]:
256282
logger.debug(f"Post has title: {post.get('title') is not None}")
257283
logger.debug(f"Post has post_date: {post.get('post_date') is not None}")
258284

259-
# Check if it's actually a draft (not published)
260-
# Drafts have type='draft' or no post_date
261-
is_draft = (
262-
post.get('type') == 'draft' or
263-
(not post.get('post_date') and (post.get('draft_title') or post.get('title')))
264-
)
265-
266-
if is_draft:
267-
drafts.append(post)
268-
if len(drafts) >= limit:
269-
break
285+
# For debugging: add ALL posts to see what we're getting
286+
drafts.append(post)
287+
if len(drafts) >= limit:
288+
break
270289

271290
logger.info(f"Returning {len(drafts)} drafts")
272291
return drafts

src/server.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,20 +440,25 @@ async def handle_call_tool(name: str, arguments: Optional[Dict[str, Any]]) -> Li
440440
)]
441441

442442
elif name == "list_drafts":
443+
logger.info(f"list_drafts called with arguments: {arguments}")
443444
post_handler = PostHandler(client)
444445
drafts = await post_handler.list_drafts(
445446
limit=arguments.get("limit", 10)
446447
)
448+
logger.info(f"list_drafts returned {len(drafts)} drafts")
447449

448450
draft_list = []
449451
for draft in drafts:
450452
title = draft.get('draft_title') or draft.get('title') or 'Untitled'
451453
draft_id = draft.get('id')
452454
draft_list.append(f"- {title} (ID: {draft_id})")
453455

456+
response_text = f"Found {len(drafts)} drafts:\n" + "\n".join(draft_list)
457+
logger.info(f"Returning response: {response_text[:100]}...")
458+
454459
return [TextContent(
455460
type="text",
456-
text=f"Found {len(drafts)} drafts:\n" + "\n".join(draft_list)
461+
text=response_text
457462
)]
458463

459464
elif name == "upload_image":
@@ -814,7 +819,9 @@ async def handle_call_tool(name: str, arguments: Optional[Dict[str, Any]]) -> Li
814819

815820
async def run(self):
816821
"""Run the MCP server using stdio transport"""
822+
logger.info("Starting MCP server...")
817823
async with stdio_server() as (read_stream, write_stream):
824+
logger.info("Stdio transport established")
818825
await self.server.run(
819826
read_stream,
820827
write_stream,
@@ -824,6 +831,7 @@ async def run(self):
824831
capabilities={}
825832
)
826833
)
834+
logger.info("Server run completed")
827835

828836

829837
def main():

test_simple_mcp.sh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/bin/bash
2+
# Simple test of MCP server with initialization
3+
4+
export SUBSTACK_PUBLICATION_URL="https://neroaugustus.substack.com/"
5+
6+
# Test with proper MCP initialization sequence
7+
echo '{"jsonrpc":"2.0","method":"initialize","params":{"protocolVersion":"0.1.0","capabilities":{},"clientInfo":{"name":"test","version":"1.0.0"}},"id":1}
8+
{"jsonrpc":"2.0","method":"initialized","params":{}}
9+
{"jsonrpc":"2.0","method":"tools/call","params":{"name":"list_drafts","arguments":{"limit":5}},"id":2}' | /opt/homebrew/bin/substack-mcp-plus

0 commit comments

Comments
 (0)