Skip to content

Commit a0a89bb

Browse files
committed
save commit
1 parent 27989ca commit a0a89bb

27 files changed

Lines changed: 391 additions & 335 deletions

examples/client/src/elicitationUrlExample.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ import {
2626
ErrorCode,
2727
getDisplayName,
2828
ListToolsResultSchema,
29-
McpError,
29+
ProtocolError,
3030
StreamableHTTPClientTransport,
3131
UnauthorizedError,
3232
UrlElicitationRequiredError
@@ -339,7 +339,7 @@ async function handleElicitationRequest(request: ElicitRequest): Promise<ElicitR
339339
} else {
340340
// Should not happen because the client declares its capabilities to the server,
341341
// but being defensive is a good practice:
342-
throw new McpError(ErrorCode.InvalidParams, `Unsupported elicitation mode: ${mode}`);
342+
throw new ProtocolError(ErrorCode.InvalidParams, `Unsupported elicitation mode: ${mode}`);
343343
}
344344
}
345345

examples/client/src/simpleStreamableHttp.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {
2121
ListResourcesResultSchema,
2222
ListToolsResultSchema,
2323
LoggingMessageNotificationSchema,
24-
McpError,
24+
ProtocolError,
2525
ReadResourceResultSchema,
2626
RELATED_TASK_META_KEY,
2727
ResourceListChangedNotificationSchema,
@@ -274,7 +274,7 @@ async function connect(url?: string): Promise<void> {
274274
// Set up elicitation request handler with proper validation
275275
client.setRequestHandler(ElicitRequestSchema, async request => {
276276
if (request.params.mode !== 'form') {
277-
throw new McpError(ErrorCode.InvalidParams, `Unsupported elicitation mode: ${request.params.mode}`);
277+
throw new ProtocolError(ErrorCode.InvalidParams, `Unsupported elicitation mode: ${request.params.mode}`);
278278
}
279279
console.log('\n🔔 Elicitation (form) Request Received:');
280280
console.log(`Message: ${request.params.message}`);

examples/client/src/simpleTaskInteractiveClient.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
ElicitRequestSchema,
1818
ErrorCode,
1919
isTextContent,
20-
McpError,
20+
ProtocolError,
2121
StreamableHTTPClientTransport
2222
} from '@modelcontextprotocol/client';
2323

@@ -105,7 +105,7 @@ async function run(url: string): Promise<void> {
105105
// Set up elicitation request handler
106106
client.setRequestHandler(ElicitRequestSchema, async request => {
107107
if (request.params.mode && request.params.mode !== 'form') {
108-
throw new McpError(ErrorCode.InvalidParams, `Unsupported elicitation mode: ${request.params.mode}`);
108+
throw new ProtocolError(ErrorCode.InvalidParams, `Unsupported elicitation mode: ${request.params.mode}`);
109109
}
110110
return elicitationCallback(request.params);
111111
});

examples/server/src/simpleStreamableHttp.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ const getServer = () => {
5050
);
5151

5252
// Enable task support via TaskPlugin
53-
server.server.usePlugin(
53+
server.usePlugin(
5454
new TaskPlugin({
5555
taskStore,
5656
taskMessageQueue: new InMemoryTaskMessageQueue()

packages/client/src/client/client.ts

Lines changed: 128 additions & 60 deletions
Large diffs are not rendered by default.

packages/client/src/client/sse.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { FetchLike, JSONRPCMessage, Transport } from '@modelcontextprotocol/core';
2-
import { createFetchWithInit, JSONRPCMessageSchema, normalizeHeaders } from '@modelcontextprotocol/core';
2+
import { createFetchWithInit, JSONRPCMessageSchema, normalizeHeaders, StateError } from '@modelcontextprotocol/core';
33
import type { ErrorEvent, EventSourceInit } from 'eventsource';
44
import { EventSource } from 'eventsource';
55

@@ -211,7 +211,7 @@ export class SSEClientTransport implements Transport {
211211

212212
async start() {
213213
if (this._eventSource) {
214-
throw new Error('SSEClientTransport already started! If using Client class, note that connect() calls start() automatically.');
214+
throw StateError.alreadyConnected();
215215
}
216216

217217
return await this._startOrAuth();
@@ -245,7 +245,7 @@ export class SSEClientTransport implements Transport {
245245

246246
async send(message: JSONRPCMessage): Promise<void> {
247247
if (!this._endpoint) {
248-
throw new Error('Not connected');
248+
throw StateError.notConnected('send message');
249249
}
250250

251251
try {

packages/client/src/client/stdio.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import type { Stream } from 'node:stream';
44
import { PassThrough } from 'node:stream';
55

66
import type { JSONRPCMessage, Transport } from '@modelcontextprotocol/core';
7-
import { ReadBuffer, serializeMessage } from '@modelcontextprotocol/core';
7+
import { ReadBuffer, serializeMessage, StateError } from '@modelcontextprotocol/core';
88
import spawn from 'cross-spawn';
99

1010
export type StdioServerParameters = {
@@ -112,9 +112,7 @@ export class StdioClientTransport implements Transport {
112112
*/
113113
async start(): Promise<void> {
114114
if (this._process) {
115-
throw new Error(
116-
'StdioClientTransport already started! If using Client class, note that connect() calls start() automatically.'
117-
);
115+
throw StateError.alreadyConnected();
118116
}
119117

120118
return new Promise((resolve, reject) => {
@@ -246,7 +244,7 @@ export class StdioClientTransport implements Transport {
246244
send(message: JSONRPCMessage): Promise<void> {
247245
return new Promise(resolve => {
248246
if (!this._process?.stdin) {
249-
throw new Error('Not connected');
247+
throw StateError.notConnected('send message');
250248
}
251249

252250
const json = serializeMessage(message);

packages/client/src/client/streamableHttp.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import {
77
isJSONRPCRequest,
88
isJSONRPCResultResponse,
99
JSONRPCMessageSchema,
10-
normalizeHeaders
10+
normalizeHeaders,
11+
StateError
1112
} from '@modelcontextprotocol/core';
1213
import { EventSourceParserStream } from 'eventsource-parser/stream';
1314

@@ -422,9 +423,7 @@ export class StreamableHTTPClientTransport implements Transport {
422423

423424
async start() {
424425
if (this._abortController) {
425-
throw new Error(
426-
'StreamableHTTPClientTransport already started! If using Client class, note that connect() calls start() automatically.'
427-
);
426+
throw StateError.alreadyConnected();
428427
}
429428

430429
this._abortController = new AbortController();

packages/client/src/client/websocket.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { JSONRPCMessage, Transport } from '@modelcontextprotocol/core';
2-
import { JSONRPCMessageSchema } from '@modelcontextprotocol/core';
2+
import { JSONRPCMessageSchema, StateError } from '@modelcontextprotocol/core';
33

44
const SUBPROTOCOL = 'mcp';
55

@@ -20,9 +20,7 @@ export class WebSocketClientTransport implements Transport {
2020

2121
start(): Promise<void> {
2222
if (this._socket) {
23-
throw new Error(
24-
'WebSocketClientTransport already started! If using Client class, note that connect() calls start() automatically.'
25-
);
23+
throw StateError.alreadyConnected();
2624
}
2725

2826
return new Promise((resolve, reject) => {
@@ -63,7 +61,7 @@ export class WebSocketClientTransport implements Transport {
6361
send(message: JSONRPCMessage): Promise<void> {
6462
return new Promise((resolve, reject) => {
6563
if (!this._socket) {
66-
reject(new Error('Not connected'));
64+
reject(StateError.notConnected('send message'));
6765
return;
6866
}
6967

packages/client/src/experimental/tasks/client.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import type {
2020
Result,
2121
SchemaOutput
2222
} from '@modelcontextprotocol/core';
23-
import { CallToolResultSchema, ErrorCode, McpError, TaskClientPlugin } from '@modelcontextprotocol/core';
23+
import { CallToolResultSchema, ErrorCode, ProtocolError, TaskClientPlugin } from '@modelcontextprotocol/core';
2424

2525
import type { Client } from '../../client/client.js';
2626

@@ -62,7 +62,7 @@ export class ExperimentalClientTasks<
6262
private _getTaskClient(): TaskClientPlugin {
6363
const plugin = this._client.getPlugin(TaskClientPlugin);
6464
if (!plugin) {
65-
throw new McpError(
65+
throw new ProtocolError(
6666
ErrorCode.InternalError,
6767
'TaskClientPlugin not installed. Use client.usePlugin(new TaskClientPlugin()) first.'
6868
);
@@ -137,7 +137,7 @@ export class ExperimentalClientTasks<
137137
if (!result.structuredContent && !result.isError) {
138138
yield {
139139
type: 'error',
140-
error: new McpError(
140+
error: new ProtocolError(
141141
ErrorCode.InvalidRequest,
142142
`Tool ${params.name} has an output schema but did not return structured content`
143143
)
@@ -154,21 +154,21 @@ export class ExperimentalClientTasks<
154154
if (!validationResult.valid) {
155155
yield {
156156
type: 'error',
157-
error: new McpError(
157+
error: new ProtocolError(
158158
ErrorCode.InvalidParams,
159159
`Structured content does not match the tool's output schema: ${validationResult.errorMessage}`
160160
)
161161
};
162162
return;
163163
}
164164
} catch (error) {
165-
if (error instanceof McpError) {
165+
if (error instanceof ProtocolError) {
166166
yield { type: 'error', error };
167167
return;
168168
}
169169
yield {
170170
type: 'error',
171-
error: new McpError(
171+
error: new ProtocolError(
172172
ErrorCode.InvalidParams,
173173
`Failed to validate structured content: ${error instanceof Error ? error.message : String(error)}`
174174
)
@@ -208,7 +208,7 @@ export class ExperimentalClientTasks<
208208
*/
209209
async getTaskResult<T extends AnyObjectSchema>(taskId: string, resultSchema?: T, options?: RequestOptions): Promise<SchemaOutput<T>> {
210210
if (!resultSchema) {
211-
throw new McpError(ErrorCode.InvalidParams, 'resultSchema is required');
211+
throw new ProtocolError(ErrorCode.InvalidParams, 'resultSchema is required');
212212
}
213213
return this._getTaskClient().getTaskResult({ taskId }, resultSchema, options);
214214
}

0 commit comments

Comments
 (0)