@ai-sdk/mcp 1.0.36 → 1.0.38

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.
@@ -1,18 +1,18 @@
1
1
  import {
2
2
  EventSourceParserStream,
3
- FetchFunction,
4
3
  withUserAgentSuffix,
5
4
  getRuntimeEnvironmentUserAgent,
5
+ type FetchFunction,
6
6
  } from '@ai-sdk/provider-utils';
7
7
  import { MCPClientError } from '../error/mcp-client-error';
8
- import { JSONRPCMessage, JSONRPCMessageSchema } from './json-rpc-message';
9
- import { MCPTransport } from './mcp-transport';
8
+ import { parseJSONRPCMessage, type JSONRPCMessage } from './json-rpc-message';
9
+ import type { MCPTransport } from './mcp-transport';
10
10
  import { VERSION } from '../version';
11
11
  import {
12
- OAuthClientProvider,
13
12
  extractResourceMetadataUrl,
14
13
  UnauthorizedError,
15
14
  auth,
15
+ type OAuthClientProvider,
16
16
  } from './oauth';
17
17
  import { LATEST_PROTOCOL_VERSION } from './types';
18
18
 
@@ -168,9 +168,7 @@ export class SseMCPTransport implements MCPTransport {
168
168
  resolve();
169
169
  } else if (event === 'message') {
170
170
  try {
171
- const message = JSONRPCMessageSchema.parse(
172
- JSON.parse(data),
173
- );
171
+ const message = await parseJSONRPCMessage(data);
174
172
  this.onmessage?.(message);
175
173
  } catch (error) {
176
174
  const e = new MCPClientError({
@@ -280,6 +278,8 @@ export class SseMCPTransport implements MCPTransport {
280
278
  }
281
279
  }
282
280
 
283
- export function deserializeMessage(line: string): JSONRPCMessage {
284
- return JSONRPCMessageSchema.parse(JSON.parse(line));
281
+ export async function deserializeMessage(
282
+ line: string,
283
+ ): Promise<JSONRPCMessage> {
284
+ return parseJSONRPCMessage(line);
285
285
  }
@@ -1,6 +1,6 @@
1
- import { ChildProcess, spawn } from 'node:child_process';
1
+ import { spawn, type ChildProcess } from 'node:child_process';
2
2
  import { getEnvironment } from './get-environment';
3
- import { StdioConfig } from './mcp-stdio-transport';
3
+ import type { StdioConfig } from './mcp-stdio-transport';
4
4
 
5
5
  export function createChildProcess(
6
6
  config: StdioConfig,
@@ -1,7 +1,7 @@
1
1
  import type { ChildProcess, IOType } from 'node:child_process';
2
- import { Stream } from 'node:stream';
3
- import { JSONRPCMessage, JSONRPCMessageSchema } from '../json-rpc-message';
4
- import { MCPTransport } from '../mcp-transport';
2
+ import type { Stream } from 'node:stream';
3
+ import { parseJSONRPCMessage, type JSONRPCMessage } from '../json-rpc-message';
4
+ import type { MCPTransport } from '../mcp-transport';
5
5
  import { MCPClientError } from '../../error/mcp-client-error';
6
6
  import { createChildProcess } from './create-child-process';
7
7
 
@@ -68,7 +68,7 @@ export class StdioMCPTransport implements MCPTransport {
68
68
 
69
69
  this.process.stdout?.on('data', chunk => {
70
70
  this.readBuffer.append(chunk);
71
- this.processReadBuffer();
71
+ void this.processReadBuffer();
72
72
  });
73
73
 
74
74
  this.process.stdout?.on('error', error => {
@@ -81,14 +81,15 @@ export class StdioMCPTransport implements MCPTransport {
81
81
  });
82
82
  }
83
83
 
84
- private processReadBuffer() {
84
+ private async processReadBuffer() {
85
85
  while (true) {
86
- try {
87
- const message = this.readBuffer.readMessage();
88
- if (message === null) {
89
- break;
90
- }
86
+ const line = this.readBuffer.readLine();
87
+ if (line === null) {
88
+ break;
89
+ }
91
90
 
91
+ try {
92
+ const message = await deserializeMessage(line);
92
93
  this.onmessage?.(message);
93
94
  } catch (error) {
94
95
  this.onerror?.(error as Error);
@@ -127,7 +128,7 @@ class ReadBuffer {
127
128
  this.buffer = this.buffer ? Buffer.concat([this.buffer, chunk]) : chunk;
128
129
  }
129
130
 
130
- readMessage(): JSONRPCMessage | null {
131
+ readLine(): string | null {
131
132
  if (!this.buffer) return null;
132
133
 
133
134
  const index = this.buffer.indexOf('\n');
@@ -137,7 +138,7 @@ class ReadBuffer {
137
138
 
138
139
  const line = this.buffer.toString('utf8', 0, index);
139
140
  this.buffer = this.buffer.subarray(index + 1);
140
- return deserializeMessage(line);
141
+ return line;
141
142
  }
142
143
 
143
144
  clear(): void {
@@ -149,6 +150,8 @@ function serializeMessage(message: JSONRPCMessage): string {
149
150
  return JSON.stringify(message) + '\n';
150
151
  }
151
152
 
152
- export function deserializeMessage(line: string): JSONRPCMessage {
153
- return JSONRPCMessageSchema.parse(JSON.parse(line));
153
+ export async function deserializeMessage(
154
+ line: string,
155
+ ): Promise<JSONRPCMessage> {
156
+ return parseJSONRPCMessage(line);
154
157
  }
@@ -1,9 +1,9 @@
1
- import { FetchFunction } from '@ai-sdk/provider-utils';
1
+ import type { FetchFunction } from '@ai-sdk/provider-utils';
2
2
  import { MCPClientError } from '../error/mcp-client-error';
3
- import { JSONRPCMessage } from './json-rpc-message';
3
+ import type { JSONRPCMessage } from './json-rpc-message';
4
4
  import { SseMCPTransport } from './mcp-sse-transport';
5
5
  import { HttpMCPTransport } from './mcp-http-transport';
6
- import { OAuthClientProvider } from './oauth';
6
+ import type { OAuthClientProvider } from './oauth';
7
7
 
8
8
  /**
9
9
  * Transport interface for MCP (Model Context Protocol) communication.
@@ -1,13 +1,13 @@
1
1
  import { delay } from '@ai-sdk/provider-utils';
2
- import { JSONRPCMessage } from './json-rpc-message';
3
- import { MCPTransport } from './mcp-transport';
2
+ import type { JSONRPCMessage } from './json-rpc-message';
3
+ import type { MCPTransport } from './mcp-transport';
4
4
  import {
5
- MCPTool,
6
- MCPResource,
7
- MCPPrompt,
8
5
  GetPromptResult,
9
- CallToolResult,
10
6
  LATEST_PROTOCOL_VERSION,
7
+ type MCPTool,
8
+ type MCPResource,
9
+ type MCPPrompt,
10
+ type CallToolResult,
11
11
  } from './types';
12
12
 
13
13
  const DEFAULT_TOOLS: MCPTool[] = [
package/src/tool/oauth.ts CHANGED
@@ -1,17 +1,17 @@
1
1
  import pkceChallenge from 'pkce-challenge';
2
2
  import {
3
- OAuthTokens,
4
- OAuthProtectedResourceMetadata,
5
3
  OAuthProtectedResourceMetadataSchema,
6
4
  OAuthMetadataSchema,
7
5
  OpenIdProviderDiscoveryMetadataSchema,
8
- AuthorizationServerMetadata,
9
- OAuthClientInformation,
10
6
  OAuthTokensSchema,
11
7
  OAuthErrorResponseSchema,
12
- OAuthClientMetadata,
13
- OAuthClientInformationFull,
14
8
  OAuthClientInformationFullSchema,
9
+ type OAuthTokens,
10
+ type OAuthProtectedResourceMetadata,
11
+ type AuthorizationServerMetadata,
12
+ type OAuthClientInformation,
13
+ type OAuthClientMetadata,
14
+ type OAuthClientInformationFull,
15
15
  } from './oauth-types';
16
16
  import {
17
17
  MCPClientOAuthError,
@@ -27,7 +27,7 @@ import {
27
27
  resourceUrlStripSlash,
28
28
  } from '../util/oauth-util';
29
29
  import { LATEST_PROTOCOL_VERSION } from './types';
30
- import { FetchFunction } from '@ai-sdk/provider-utils';
30
+ import { parseJSON, type FetchFunction } from '@ai-sdk/provider-utils';
31
31
 
32
32
  export type AuthResult = 'AUTHORIZED' | 'REDIRECT';
33
33
 
@@ -591,7 +591,9 @@ export async function parseErrorResponse(
591
591
  const body = input instanceof Response ? await input.text() : input;
592
592
 
593
593
  try {
594
- const result = OAuthErrorResponseSchema.parse(JSON.parse(body));
594
+ const result = OAuthErrorResponseSchema.parse(
595
+ await parseJSON({ text: body }),
596
+ );
595
597
  const { error, error_description, error_uri } = result;
596
598
  const errorClass = OAUTH_ERRORS[error] || ServerError;
597
599
  return new errorClass({
package/src/tool/types.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import { z } from 'zod/v4';
2
- import { JSONObject } from '@ai-sdk/provider';
3
- import { FlexibleSchema, Tool } from '@ai-sdk/provider-utils';
2
+ import type { JSONObject } from '@ai-sdk/provider';
3
+ import type { FlexibleSchema, Tool } from '@ai-sdk/provider-utils';
4
4
 
5
5
  export const LATEST_PROTOCOL_VERSION = '2025-11-25';
6
6
  export const SUPPORTED_PROTOCOL_VERSIONS = [