AT-360 Added Diagram Chat Functionality To Mermaid/sdk So MCP Server Can Directly Access Mermaid AI #43
Conversation
…at with mermaid ai
|
The latest updates on your projects. Learn more about Vercel for GitHub. 1 Skipped Deployment
|
There was a problem hiding this comment.
Pull request overview
Adds SDK support for a new Mermaid AI “diagram chat” endpoint so consumers (e.g., an MCP server) can send a user prompt + optional diagram context and receive streamed AI output.
Changes:
- Added
/rest-api/openai/chatURL constant. - Introduced
DiagramChatRequest/DiagramChatResponsepublic types. - Implemented
MermaidChart.diagramChat()including parsing of Vercel AI SDK data-stream output.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 5 comments.
| File | Description |
|---|---|
| packages/sdk/src/urls.ts | Adds the REST URL for the new OpenAI chat endpoint. |
| packages/sdk/src/types.ts | Defines request/response types for diagram chat. |
| packages/sdk/src/index.ts | Implements diagramChat() and helpers to parse the streamed response body. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Comments suppressed due to low confidence (1)
packages/sdk/src/index.ts:24
- The PR description says the new DiagramChatRequest/DiagramChatResponse types are exposed for SDK consumers, but the entrypoint doesn’t re-export them. Since this package’s public surface is
src/index.ts(anddist/index.d.ts), consider adding explicitexport type { DiagramChatRequest, DiagramChatResponse } from './types.js'so consumers can import them from@mermaidchart/sdkwithout deep imports.
import type {
AuthState,
AuthorizationData,
DiagramChatRequest,
DiagramChatResponse,
Document,
InitParams,
MCDocument,
MCProject,
MCUser,
RepairDiagramRequest,
RepairDiagramResponse,
AICreditsUsage,
} from './types.js';
import { URLS } from './urls.js';
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
packages/sdk/src/index.test.ts
Outdated
| vi.spyOn(client, 'diagramChat').mockRejectedValue( | ||
| new AICreditsLimitExceededError('AI credits limit exceeded'), | ||
| ); |
There was a problem hiding this comment.
This test doesn’t exercise the 402-handling logic in diagramChat() because it mocks client.diagramChat() itself to reject. Instead, mock the underlying axios .post() to reject with a { response: { status: 402, data: ... } } shape and assert that diagramChat() maps it to AICreditsLimitExceededError.
| vi.spyOn(client, 'diagramChat').mockRejectedValue( | |
| new AICreditsLimitExceededError('AI credits limit exceeded'), | |
| ); | |
| // Mock the underlying axios call to simulate a 402 response from the API. | |
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | |
| vi.spyOn((client as any).axios, 'post').mockRejectedValue({ | |
| response: { | |
| status: 402, | |
| data: { | |
| message: 'AI credits limit exceeded', | |
| }, | |
| }, | |
| }); |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| // can parse the Vercel AI SDK data-stream format after the request completes. | ||
| const response = await this.axios.post<string>(URLS.rest.openai.chat, requestBody, { | ||
| responseType: 'text', | ||
| timeout: 120_000, |
There was a problem hiding this comment.
diagramChat() hard-codes a 120s Axios timeout, which bypasses the instance-level requestTimeout configured via InitParams (and used when creating the Axios client). Consider respecting this.requestTimeout (or using something like max(this.requestTimeout, 120_000) and documenting it) so callers can control request timing consistently across SDK methods.
| timeout: 120_000, | |
| timeout: Math.max(this.requestTimeout ?? 0, 120_000), |
| } catch (error: unknown) { | ||
| if ( | ||
| error && | ||
| typeof error === 'object' && | ||
| 'response' in error && | ||
| error.response && | ||
| typeof error.response === 'object' && | ||
| 'status' in error.response && | ||
| (error as { response: { status: number } }).response.status === 402 | ||
| ) { | ||
| const axiosError = error as { response: { status: number; data?: unknown } }; | ||
| throw new AICreditsLimitExceededError( | ||
| typeof axiosError.response.data === 'string' | ||
| ? axiosError.response.data | ||
| : 'AI credits limit exceeded', | ||
| ); | ||
| } |
There was a problem hiding this comment.
The 402->AICreditsLimitExceededError mapping logic is now duplicated in both repairDiagram() and diagramChat(). To reduce divergence risk (e.g., message formatting changes), consider extracting a small shared helper (or a private method) to detect the 402 Axios shape and build the error consistently.
This pull request adds a diagramChat method to the @mermaidchart/sdk, enabling external clients such as MCP servers to chat directly with Mermaid AI without handling streaming internals themselves.
These changes also expose the necessary types (DiagramChatRequest, DiagramChatResponse) so consumers get full TypeScript support for the new chat capability.
New diagramChat method in SDK: