Hi team,
I am facing an issue with the Python SDK for Claude Code MCP integration with Zerodha.
Even after authenticating and obtaining a session ID, subsequent calls (such as fetching holdings or positions) do not work as expected. The SDK asks me to authenticate again, instead of using the existing session. This is not the case when I use the Claude Code CLI, where the session persists and I can continue making authenticated calls.
Steps to reproduce:
Run the following script (see below for full code).
Authenticate in the browser when prompted.
After entering the session ID, the script attempts to fetch holdings and positions.
Instead of using the authenticated session, the SDK again asks for fresh authentication.
Expected behavior:
After authenticating once and obtaining a session ID, I should be able to use that session for subsequent API calls without being prompted to re-authenticate.
Actual behavior:
The SDK asks for authentication again for every call, as if the session is not being persisted or recognized.
Sample Code:
import anyio
from claude_code_sdk import (
AssistantMessage,
ClaudeCodeOptions,
ResultMessage,
TextBlock,
query,
)
async def login_and_get_session():
session_id = None
options = ClaudeCodeOptions(
allowed_tools=["mcp__zerodha_mcp__login", "mcp__zerodha_mcp__get_profile", "mcp__zerodha_mcp__get_holdings", "mcp__zerodha_mcp__get_positions"],
mcp_tools=["zerodha_mcp"],
permission_mode="bypassPermissions",
continue_conversation=True,
resume=session_id,
system_prompt="You are a helpful assistant",
)
async for message in query(
prompt="Please use the mcp__zerodha_mcp__login tool to login to Zerodha.",
options=options,
):
if isinstance(message, ResultMessage):
session_id = message.session_id
return session_id
async def fetch_holdings_with_session(session_id: str):
options = ClaudeCodeOptions(
allowed_tools=["mcp__zerodha_mcp__login", "mcp__zerodha_mcp__get_profile", "mcp__zerodha_mcp__get_holdings", "mcp__zerodha_mcp__get_positions"],
mcp_tools=["zerodha_mcp"],
permission_mode="bypassPermissions",
continue_conversation=True,
resume=session_id,
system_prompt="You are a helpful assistant that has already logged into Zerodha. Use the session to fetch data without re-authenticating.",
)
async for message in query(
prompt="Please use the mcp__zerodha_mcp__get_holdings tool to fetch my holdings and show them in a nice format. I am already logged in and authenticated.",
options=options,
):
if isinstance(message, AssistantMessage):
for block in message.content:
if isinstance(block, TextBlock):
print(f"Claude: {block.text}")
async def main():
session_id = await login_and_get_session()
input("Press Enter after you have completed authentication in your browser...")
if session_id:
await fetch_holdings_with_session(session_id)
if __name__ == "__main__":
anyio.run(main)
Notes:
- This works as expected in the Claude Code CLI, but not in the Python SDK.
- The session ID is correctly printed and passed to subsequent calls.
Environment:
OS: macOS
Python 3.11.9
Please let me know if you need any more details or logs.
Hi team,
I am facing an issue with the Python SDK for Claude Code MCP integration with Zerodha.
Even after authenticating and obtaining a session ID, subsequent calls (such as fetching holdings or positions) do not work as expected. The SDK asks me to authenticate again, instead of using the existing session. This is not the case when I use the Claude Code CLI, where the session persists and I can continue making authenticated calls.
Steps to reproduce:
Run the following script (see below for full code).
Authenticate in the browser when prompted.
After entering the session ID, the script attempts to fetch holdings and positions.
Instead of using the authenticated session, the SDK again asks for fresh authentication.
Expected behavior:
After authenticating once and obtaining a session ID, I should be able to use that session for subsequent API calls without being prompted to re-authenticate.
Actual behavior:
The SDK asks for authentication again for every call, as if the session is not being persisted or recognized.
Sample Code:
Notes:
Environment:
OS: macOS
Python 3.11.9
Please let me know if you need any more details or logs.