1313// limitations under the License.
1414
1515import { StitchProxyConfig } from "../spec/proxy.js" ;
16- import { buildAuthHeaders } from "../auth .js" ;
16+ import type { StitchToolClient } from "../client .js" ;
1717import type { Tool } from "@modelcontextprotocol/sdk/types.js" ;
18- import { repairToolSchemas } from "../schema-repair.js" ;
1918
2019/**
2120 * Shared state for proxy handlers.
21+ *
22+ * `client` is the ONE MCP stack in the repo (D9): a real StitchToolClient
23+ * over the MCP SDK's StreamableHTTPClientTransport. The caller constructs
24+ * and injects it (see core.ts) — this module never builds transports or
25+ * speaks JSON-RPC itself.
2226 */
2327export interface ProxyContext {
2428 config : StitchProxyConfig ;
29+ client : StitchToolClient ;
2530 remoteTools : Tool [ ] ;
2631}
2732
2833/**
29- * Forward a JSON-RPC request to Stitch.
30- */
31- export async function forwardToStitch (
32- config : StitchProxyConfig ,
33- method : string ,
34- params ?: unknown ,
35- ) : Promise < unknown > {
36- const request = {
37- jsonrpc : "2.0" ,
38- method,
39- params : params ?? { } ,
40- id : Date . now ( ) ,
41- } ;
42-
43- let response : Response ;
44- try {
45- response = await fetch ( config . url , {
46- method : "POST" ,
47- headers : {
48- "Content-Type" : "application/json" ,
49- Accept : "application/json" ,
50- ...buildAuthHeaders ( config ) ,
51- } ,
52- body : JSON . stringify ( request ) ,
53- } ) ;
54- } catch ( err : any ) {
55- throw new Error ( `Network failure connecting to Stitch API: ${ err . message } ` ) ;
56- }
57-
58- if ( ! response . ok ) {
59- const errorText = await response . text ( ) ;
60- throw new Error ( `Stitch API error (${ response . status } ): ${ errorText } ` ) ;
61- }
62-
63- const result = ( await response . json ( ) ) as {
64- error ?: { message : string } ;
65- result ?: unknown ;
66- } ;
67-
68- if ( result . error ) {
69- throw new Error ( `Stitch RPC error: ${ result . error . message } ` ) ;
70- }
71-
72- return result . result ;
73- }
74-
75- /**
76- * Initialize connection to Stitch and fetch tools.
34+ * Initialize the upstream Stitch connection and fetch tools.
35+ *
36+ * The MCP SDK handles the initialize handshake (protocol version,
37+ * session id, awaited notifications/initialized) — the hand-rolled
38+ * JSON-RPC machinery that used to live here is gone.
7739 */
7840export async function initializeStitchConnection (
7941 ctx : ProxyContext ,
8042) : Promise < void > {
81- // Send initialize request
82- await forwardToStitch ( ctx . config , "initialize" , {
83- protocolVersion : ctx . config . protocolVersion || "2024-11-05" ,
84- capabilities : { } ,
85- clientInfo : {
86- name : ctx . config . name ,
87- version : ctx . config . version ,
88- } ,
89- } ) ;
90-
91- // Send initialized notification (fire and forget)
92- fetch ( ctx . config . url , {
93- method : "POST" ,
94- headers : {
95- "Content-Type" : "application/json" ,
96- Accept : "application/json" ,
97- ...buildAuthHeaders ( ctx . config ) ,
98- } ,
99- body : JSON . stringify ( {
100- jsonrpc : "2.0" ,
101- method : "notifications/initialized" ,
102- } ) ,
103- } ) . catch ( ( err ) => {
104- console . error (
105- "[stitch-proxy] Failed to send initialized notification:" ,
106- err ,
107- ) ;
108- } ) ;
109-
43+ await ctx . client . connect ( ) ;
11044 await refreshTools ( ctx ) ;
11145 console . error (
11246 `[stitch-proxy] Connected to Stitch, discovered ${ ctx . remoteTools . length } tools` ,
@@ -116,15 +50,12 @@ export async function initializeStitchConnection(
11650/**
11751 * Refresh the cached tools list from Stitch.
11852 *
119- * Applies schema repair to inject missing $defs before the tools are
120- * re-served to MCP clients whose AJV validators would otherwise crash
121- * on unresolved $ref targets.
53+ * Stores schemas RAW, exactly as served. Repair (injecting missing
54+ * $defs) is a SERVING concern applied where schemas are consumed —
55+ * see handlers/listTools.ts — never at capture: the tools-manifest
56+ * is the pipeline's source of truth and must not be coupled to the
57+ * repair heuristics.
12258 */
12359export async function refreshTools ( ctx : ProxyContext ) : Promise < void > {
124- const toolsResult = ( await forwardToStitch ( ctx . config , "tools/list" , { } ) ) as {
125- tools : Tool [ ] ;
126- } ;
127- const tools = toolsResult . tools || [ ] ;
128- repairToolSchemas ( tools ) ;
129- ctx . remoteTools = tools ;
60+ ctx . remoteTools = ( await ctx . client . listToolsRaw ( ) ) . tools ;
13061}
0 commit comments