Skip to content

feat(claude): add sendChatHistoryAsync for Claude Agent SDK#258

Open
gwharris7 wants to merge 9 commits into
mainfrom
gwharris7/claude-sdk-chat-history
Open

feat(claude): add sendChatHistoryAsync for Claude Agent SDK#258
gwharris7 wants to merge 9 commits into
mainfrom
gwharris7/claude-sdk-chat-history

Conversation

@gwharris7
Copy link
Copy Markdown
Contributor

@gwharris7 gwharris7 commented Jun 4, 2026

Summary

Implements a Claude-specific sendChatHistoryAsync method that mirrors the OpenAI implementation from PR #157, resolving the gap tracked in issue #164.

The Claude Agent SDK (@anthropic-ai/claude-agent-sdk) now exports getSessionMessages(sessionId, options?) which returns SessionMessage[]. This PR leverages that API to provide first-class chat history support for Claude agents.

Changes

Source (packages/agents-a365-tooling-extensions-claude)

  • McpToolRegistrationService — Added two new methods:
    • sendChatHistoryAsync(turnContext, sessionId, limit?, toolOptions?) — Retrieves messages from a Claude session and sends them to the MCP platform for real-time threat protection
    • sendChatHistoryMessagesAsync(turnContext, messages, toolOptions?) — Sends pre-fetched SessionMessage[] to the MCP platform
  • Content extraction — Handles string content and structured content blocks (text, tool_use, tool_result) from the Anthropic API message format
  • Role normalization — Maps Claude's type field (user, assistant, system) to standard chat roles
  • index.ts — Re-exports getSessionMessages, SessionMessage, and GetSessionMessagesOptions for convenience

Dependencies

  • Upgraded @anthropic-ai/claude-agent-sdk from ^0.1.30 to ^0.2.59 (adds getSessionMessages API)
  • Added uuid dependency for fallback ID generation

Tests (tests/tooling-extensions-claude/)

  • sendChatHistoryAsync.test.ts — 16 tests covering input validation, successful scenarios, error handling, OperationResult behavior, and integration
  • messageConversion.test.ts — 12 tests covering role normalization, content extraction (string, blocks, tool calls), ID handling, and mixed content

Documentation

  • Updated docs/design.md to reflect the resolved state with usage examples and API reference

Resolves #164

Copilot AI review requested due to automatic review settings June 4, 2026 17:11
@gwharris7 gwharris7 requested review from a team as code owners June 4, 2026 17:11
Comment thread tests/tooling-extensions-claude/fixtures/mockClaudeTypes.ts Dismissed
Comment thread tests/tooling-extensions-claude/fixtures/mockClaudeTypes.ts Fixed
Comment thread tests/tooling-extensions-claude/fixtures/mockClaudeTypes.ts Fixed
Comment thread tests/tooling-extensions-claude/fixtures/mockClaudeTypes.ts Fixed
Comment thread tests/tooling-extensions-claude/fixtures/mockClaudeTypes.ts Fixed
Comment thread tests/tooling-extensions-claude/fixtures/mockClaudeTypes.ts Fixed
Comment thread tests/tooling-extensions-claude/fixtures/mockClaudeTypes.ts Fixed
@gwharris7 gwharris7 force-pushed the gwharris7/claude-sdk-chat-history branch from 0df9c99 to ee5a71e Compare June 4, 2026 17:15
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds Claude parity with the OpenAI tooling extension by introducing a Claude-specific chat history sender that pulls session messages via the upgraded Claude Agent SDK and forwards them to the MCP platform for threat protection.

Changes:

  • Implement sendChatHistoryAsync / sendChatHistoryMessagesAsync in the Claude tooling extension, including conversion from SessionMessage[] to ChatHistoryMessage[].
  • Upgrade @anthropic-ai/claude-agent-sdk to a version that exposes getSessionMessages, and re-export relevant Claude SDK helpers/types.
  • Add comprehensive unit tests and update design documentation to reflect the new Claude chat history support.

Reviewed changes

Copilot reviewed 8 out of 9 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
tests/tooling-extensions-claude/sendChatHistoryAsync.test.ts Adds unit tests for session retrieval, delegation, and error handling for sendChatHistoryAsync.
tests/tooling-extensions-claude/messageConversion.test.ts Adds unit tests for role normalization, content extraction, and ID handling during message conversion.
tests/tooling-extensions-claude/fixtures/mockClaudeTypes.ts Introduces test fixtures for creating representative Claude SessionMessage shapes (string + block content).
pnpm-workspace.yaml Bumps workspace catalog Claude SDK version to enable getSessionMessages.
pnpm-lock.yaml Updates the lockfile to the new Claude SDK graph and adds uuid / related transitive deps.
packages/agents-a365-tooling-extensions-claude/src/McpToolRegistrationService.ts Implements new Claude chat history APIs and message conversion helpers.
packages/agents-a365-tooling-extensions-claude/src/index.ts Re-exports Claude SDK getSessionMessages and types for convenience.
packages/agents-a365-tooling-extensions-claude/package.json Adds uuid and @types/uuid dependencies for fallback ID generation.
packages/agents-a365-tooling-extensions-claude/docs/design.md Updates design doc to mark issue #164 resolved and documents new APIs and conversion behavior.
Files not reviewed (1)
  • pnpm-lock.yaml: Language not supported

Comment thread packages/agents-a365-tooling-extensions-claude/src/McpToolRegistrationService.ts Outdated
Comment thread packages/agents-a365-tooling-extensions-claude/src/McpToolRegistrationService.ts Outdated
Comment thread pnpm-lock.yaml
@gwharris7 gwharris7 force-pushed the gwharris7/claude-sdk-chat-history branch from ee5a71e to 6573766 Compare June 4, 2026 17:22
Copilot AI review requested due to automatic review settings June 4, 2026 17:26
@gwharris7 gwharris7 force-pushed the gwharris7/claude-sdk-chat-history branch from 6573766 to 1e30e55 Compare June 4, 2026 17:26
@gwharris7 gwharris7 force-pushed the gwharris7/claude-sdk-chat-history branch from 1e30e55 to a5b6f83 Compare June 4, 2026 17:27
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 8 out of 9 changed files in this pull request and generated 6 comments.

Files not reviewed (1)
  • pnpm-lock.yaml: Language not supported

Comment thread pnpm-workspace.yaml
Comment thread packages/agents-a365-tooling-extensions-claude/docs/design.md
Comment thread packages/agents-a365-tooling-extensions-claude/src/McpToolRegistrationService.ts Outdated
Comment thread packages/agents-a365-tooling-extensions-claude/src/McpToolRegistrationService.ts Outdated
Comment thread pnpm-lock.yaml
@gwharris7 gwharris7 force-pushed the gwharris7/claude-sdk-chat-history branch from a5b6f83 to 537fef1 Compare June 4, 2026 17:35
Implement Claude-specific sendChatHistoryAsync and sendChatHistoryMessagesAsync
methods that mirror the OpenAI implementation from PR #157. This resolves the
gap tracked in issue #164.

Changes:
- Upgrade @anthropic-ai/claude-agent-sdk from ^0.1.30 to ^0.3.162 which
  includes the new getSessionMessages() API
- Add sendChatHistoryAsync(turnContext, sessionId, limit?, toolOptions?) that
  retrieves session messages and sends them to the MCP platform for real-time
  threat protection
- Add sendChatHistoryMessagesAsync for pre-fetched message arrays
- Handle structured content blocks (text, tool_use, tool_result)
- Normalize message types to standard roles (user, assistant, system, tool)
- Add uuid dependency for fallback ID generation
- Re-export getSessionMessages and SessionMessage from the package
- Add comprehensive tests (28 passing) covering validation, conversion,
  error handling, and integration
- Update design docs to reflect resolved state

Resolves #164

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 4, 2026 17:49
@gwharris7 gwharris7 force-pushed the gwharris7/claude-sdk-chat-history branch from 537fef1 to 4c3ba5a Compare June 4, 2026 17:49
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

gwharris7 and others added 3 commits June 4, 2026 11:18
The @anthropic-ai/claude-agent-sdk is ESM-only (type: module), which
causes Jest/ts-jest to hang when resolving imports. Added:
- moduleNameMapper for the Claude SDK in jest.config.cjs
- Manual mock at tests/__mocks__/@anthropic-ai/claude-agent-sdk.ts
- Local MockSessionMessage type in fixtures (avoids ESM type import)
- jest.requireMock pattern instead of direct import in test file
- @ts-nocheck for sendChatHistoryAsync.test.ts (requireMock types)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Pin to exact 0.2.59 to avoid platform-specific native binaries
introduced in 0.2.110. This removes:
- 8 platform-specific optional deps (@anthropic-ai/claude-agent-sdk-*)
- @anthropic-ai/sdk peer dependency
- @babel/runtime and json-schema-to-ts transitive deps
- @modelcontextprotocol/sdk (unused in Claude extension source)

Net result: +12 -51 packages in lockfile.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 5, 2026 15:07
Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 10 out of 11 changed files in this pull request and generated 6 comments.

Files not reviewed (1)
  • pnpm-lock.yaml: Language not supported

Comment thread packages/agents-a365-tooling-extensions-claude/src/McpToolRegistrationService.ts Outdated
Comment thread packages/agents-a365-tooling-extensions-claude/docs/design.md Outdated
Comment thread packages/agents-a365-tooling-extensions-claude/docs/design.md Outdated
Comment thread pnpm-workspace.yaml Outdated
Comment thread tests/tooling-extensions-claude/messageConversion.test.ts
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings June 5, 2026 15:17
gwharris7 and others added 3 commits June 5, 2026 08:17
Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 10 out of 11 changed files in this pull request and generated 3 comments.

Files not reviewed (1)
  • pnpm-lock.yaml: Language not supported

Comment thread tests/tooling-extensions-claude/messageConversion.test.ts
Comment thread pnpm-workspace.yaml
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Claude SDK: Monitor for chat history API availability

3 participants