11import * as NodeCrypto from "@effect/platform-node/NodeCrypto" ;
22import * as NodeServices from "@effect/platform-node/NodeServices" ;
33import { expect , it } from "@effect/vitest" ;
4+ import { AjvJsonSchemaValidator } from "@modelcontextprotocol/sdk/validation/ajv" ;
5+ import type { JsonSchemaType } from "@modelcontextprotocol/sdk/validation" ;
46import {
57 DEFAULT_SERVER_SETTINGS ,
68 ChatImageAttachment ,
9+ CommandId ,
710 EnvironmentId ,
811 ProviderInstanceId ,
912 RunId ,
@@ -17,8 +20,13 @@ import { McpAttachmentInput } from "./attachment/input.ts";
1720import { McpSchema , McpServer , Tool } from "effect/ai" ;
1821import { FetchHttpClient } from "effect/http" ;
1922
23+ import {
24+ OrchestratorCommandRejectedError ,
25+ OrchestratorDispatchError ,
26+ OrchestratorProjectionError ,
27+ } from "../../orchestration-v2/Orchestrator.ts" ;
28+
2029import * as ServerConfig from "../../config.ts" ;
21- import { OrchestratorProjectionError } from "../../orchestration-v2/Orchestrator.ts" ;
2230import * as ProviderAdapterRegistry from "../../orchestration-v2/ProviderAdapterRegistry.ts" ;
2331import * as ThreadManagement from "../../orchestration-v2/ThreadManagementService.ts" ;
2432import * as PreviewBrowser from "../../preview/PreviewBrowser.ts" ;
@@ -28,6 +36,7 @@ import * as SecretRequests from "../../secrets/SecretRequests.ts";
2836import * as ScheduledTaskService from "../../scheduledTasks/ScheduledTaskService.ts" ;
2937import * as McpHttpServer from "../McpHttpServer.ts" ;
3038import * as McpInvocationContext from "../McpInvocationContext.ts" ;
39+ import { dispatchFailure } from "../threadAccess.ts" ;
3140import { OrchestratorToolkit } from "./orchestrator/tools.ts" ;
3241import { PreviewToolkit } from "./preview/tools.ts" ;
3342import { PreviewControlsToolkit } from "./previewControls/tools.ts" ;
@@ -136,6 +145,8 @@ it.effect("checks capability before accessing services through the production re
136145 Effect . provideService ( McpSchema . McpServerClient , client ) ,
137146 ) ;
138147 expect ( declaredFailure ( result ) ) . toMatchObject ( { code : "capability_denied" } ) ;
148+ expect ( result . isError ) . toBe ( true ) ;
149+ expect ( result . structuredContent ) . toBeUndefined ( ) ;
139150 } ) . pipe (
140151 Effect . provide (
141152 McpHttpServer . layerThreadToolkit . pipe (
@@ -161,6 +172,22 @@ it.effect("returns a bounded public failure without serializing storage causes",
161172 code : "orchestration_error" ,
162173 message : "The operation could not be completed." ,
163174 } ) ;
175+ expect ( result . isError ) . toBe ( true ) ;
176+ expect ( result . content ) . toEqual ( [
177+ {
178+ type : "text" ,
179+ text : '{"_tag":"OrchestratorMcpFailure","code":"orchestration_error","message":"The operation could not be completed."}' ,
180+ } ,
181+ ] ) ;
182+ const definition = server . tools . find ( ( { tool } ) => tool . name === "t3_thread_organize" ) ;
183+ expect ( definition ?. tool . outputSchema ) . toBeDefined ( ) ;
184+ const validate = new AjvJsonSchemaValidator ( ) . getValidator (
185+ definition ! . tool . outputSchema ! as JsonSchemaType ,
186+ ) ;
187+ expect ( result . structuredContent ) . toBeUndefined ( ) ;
188+ expect ( validate ( { sequence : 1 } ) . valid ) . toBe ( true ) ;
189+ expect ( validate ( { code : "orchestration_error" } ) . valid ) . toBe ( false ) ;
190+ expect ( validate ( { sequence : "invalid" } ) . valid ) . toBe ( false ) ;
164191 } ) . pipe (
165192 Effect . provide (
166193 McpHttpServer . layerThreadToolkit . pipe (
@@ -182,6 +209,42 @@ it.effect("returns a bounded public failure without serializing storage causes",
182209 ) ,
183210) ;
184211
212+ it ( "bounds public command rejections and redacts internal dispatch causes" , ( ) => {
213+ const command = { commandId : CommandId . make ( "mcp-core-command" ) , commandType : "thread.settle" } ;
214+ expect (
215+ dispatchFailure ( new OrchestratorDispatchError ( { ...command , cause : "🙂" . repeat ( 1001 ) } ) )
216+ . message ,
217+ ) . toBe ( "🙂" . repeat ( 1000 ) ) ;
218+ expect (
219+ dispatchFailure (
220+ new OrchestratorCommandRejectedError ( { ...command , cause : "Run is not queued." } ) ,
221+ ) . message ,
222+ ) . toBe ( "Run is not queued." ) ;
223+ for ( const cause of [
224+ undefined ,
225+ "" ,
226+ new Error ( "private-storage-path" ) ,
227+ { message : "private-storage-path" } ,
228+ ] ) {
229+ expect ( dispatchFailure ( new OrchestratorDispatchError ( { ...command , cause } ) ) ) . toMatchObject ( {
230+ code : "orchestration_error" ,
231+ message : "The operation could not be completed." ,
232+ } ) ;
233+ expect (
234+ dispatchFailure ( new OrchestratorCommandRejectedError ( { ...command , cause } ) ) ,
235+ ) . toMatchObject ( {
236+ code : "orchestration_error" ,
237+ message : "The operation could not be completed." ,
238+ } ) ;
239+ }
240+ expect (
241+ dispatchFailure ( new OrchestratorProjectionError ( { threadId, cause : "private-storage-path" } ) ) ,
242+ ) . toMatchObject ( {
243+ code : "orchestration_error" ,
244+ message : "The operation could not be completed." ,
245+ } ) ;
246+ } ) ;
247+
185248it . effect ( "returns an HTML render reference that Codex and Claude tool rows both carry" , ( ) =>
186249 Effect . gen ( function * ( ) {
187250 const server = yield * McpServer . McpServer ;
@@ -229,6 +292,57 @@ it.effect("returns an HTML render reference that Codex and Claude tool rows both
229292 ) ,
230293) ;
231294
295+ it . effect ( "returns invalid parameter errors through the production registration" , ( ) =>
296+ Effect . gen ( function * ( ) {
297+ const server = yield * McpServer . McpServer ;
298+ const error = yield * server
299+ . callTool ( { name : "t3_thread_organize" , arguments : { action : "invalid" } } )
300+ . pipe (
301+ Effect . provideService ( McpInvocationContext . McpInvocationContext , scope ) ,
302+ Effect . provideService ( McpSchema . McpServerClient , client ) ,
303+ Effect . flip ,
304+ ) ;
305+ expect ( error . _tag ) . toBe ( "InvalidParams" ) ;
306+ } ) . pipe (
307+ Effect . provide (
308+ McpHttpServer . layerThreadToolkit . pipe (
309+ Layer . provideMerge ( McpServer . McpServer . layer ) ,
310+ Layer . provide ( NodeCrypto . layer ) ,
311+ Layer . provide ( Layer . mock ( ThreadManagement . ThreadManagementService ) ( { } ) ) ,
312+ ) ,
313+ ) ,
314+ ) ,
315+ ) ;
316+
317+ it . effect ( "keeps unexpected handler defects private through the production registration" , ( ) =>
318+ Effect . gen ( function * ( ) {
319+ const server = yield * McpServer . McpServer ;
320+ const result = yield * server
321+ . callTool ( { name : "t3_thread_organize" , arguments : { action : "pin" } } )
322+ . pipe (
323+ Effect . provideService ( McpInvocationContext . McpInvocationContext , scope ) ,
324+ Effect . provideService ( McpSchema . McpServerClient , client ) ,
325+ ) ;
326+ expect ( result . isError ) . toBe ( true ) ;
327+ expect ( result . structuredContent ) . toBeUndefined ( ) ;
328+ expect ( result . content ) . toEqual ( [
329+ { type : "text" , text : "Tool execution failed due to an internal server error." } ,
330+ ] ) ;
331+ } ) . pipe (
332+ Effect . provide (
333+ McpHttpServer . layerThreadToolkit . pipe (
334+ Layer . provideMerge ( McpServer . McpServer . layer ) ,
335+ Layer . provide ( NodeCrypto . layer ) ,
336+ Layer . provide (
337+ Layer . mock ( ThreadManagement . ThreadManagementService ) ( {
338+ getThreadShell : ( ) => Effect . die ( new Error ( "private-storage-path" ) ) ,
339+ } ) ,
340+ ) ,
341+ ) ,
342+ ) ,
343+ ) ,
344+ ) ;
345+
232346it ( "keeps MCP preference output allowlisted and Unicode-bounded" , ( ) => {
233347 const settings = {
234348 ...DEFAULT_SERVER_SETTINGS ,
0 commit comments