@@ -5,6 +5,7 @@ import type {
55 Connection ,
66 ConnectionCreateParams ,
77 ConnectionListParams ,
8+ ConnectionSetParams ,
89 ConnectionsListResponse ,
910} from "@smithery/api/resources/connections.js"
1011import { createSmitheryClient } from "../../lib/smithery-client"
@@ -14,7 +15,9 @@ import {
1415} from "../../utils/smithery-settings"
1516
1617export type { Connection , ConnectionsListResponse }
17- export type ConnectionTransport = NonNullable < Connection [ "transport" ] >
18+ export type ConnectionTarget =
19+ | ( Required < Pick < ConnectionCreateParams , "mcpUrl" > > & { server ?: never } )
20+ | ( Required < Pick < ConnectionCreateParams , "server" > > & { mcpUrl ?: never } )
1821
1922export interface Trigger {
2023 name : string
@@ -156,12 +159,12 @@ export class ConnectSession {
156159 }
157160
158161 async createConnection (
159- mcpUrl ?: string ,
162+ target ?: string | ConnectionTarget ,
160163 options : ConnectionWriteOptions = { } ,
161164 ) : Promise < Connection > {
162165 return this . smitheryClient . connections . create (
163166 this . namespace ,
164- buildConnectionBody ( mcpUrl , options ) as ConnectionCreateParams ,
167+ buildConnectionBody ( target , options ) ,
165168 )
166169 }
167170
@@ -171,20 +174,20 @@ export class ConnectSession {
171174 */
172175 async setConnection (
173176 connectionId : string ,
174- mcpUrl ?: string ,
177+ target ?: string | ConnectionTarget ,
175178 options : ConnectionWriteOptions = { } ,
176179 ) : Promise < Connection > {
177180 try {
178181 return await this . smitheryClient . connections . set (
179182 connectionId ,
180- buildConnectionSetParams ( this . namespace , mcpUrl , options ) ,
183+ buildConnectionSetParams ( this . namespace , target , options ) ,
181184 )
182185 } catch ( error ) {
183186 if ( error instanceof ConflictError && options . transport !== "uplink" ) {
184187 await this . deleteConnection ( connectionId )
185188 return this . smitheryClient . connections . set (
186189 connectionId ,
187- buildConnectionSetParams ( this . namespace , mcpUrl , options ) ,
190+ buildConnectionSetParams ( this . namespace , target , options ) ,
188191 )
189192 }
190193 throw error
@@ -255,31 +258,46 @@ export class ConnectSession {
255258 }
256259}
257260
261+ export function connectionTargetFromInput ( input : string ) : ConnectionTarget {
262+ return isHttpUrl ( input ) ? { mcpUrl : input } : { server : input }
263+ }
264+
258265function buildConnectionBody (
259- mcpUrl : string | undefined ,
266+ target : string | ConnectionTarget | undefined ,
260267 options : ConnectionWriteOptions ,
261- ) : Record < string , unknown > {
262- const body = {
263- ...( mcpUrl ? { mcpUrl } : { } ) ,
268+ ) : ConnectionCreateParams {
269+ return {
270+ ...normalizeConnectionTarget ( target ) ,
264271 ...( options . name ? { name : options . name } : { } ) ,
265272 ...( options . metadata ? { metadata : options . metadata } : { } ) ,
266273 ...( options . headers ? { headers : options . headers } : { } ) ,
267274 ...( options . transport ? { transport : options . transport } : { } ) ,
268275 }
269- return body
270276}
271277
272278function buildConnectionSetParams (
273279 namespace : string ,
274- mcpUrl : string | undefined ,
280+ target : string | ConnectionTarget | undefined ,
275281 options : ConnectionWriteOptions ,
276- ) {
282+ ) : ConnectionSetParams {
277283 return {
278284 namespace,
279- ...buildConnectionBody ( mcpUrl , options ) ,
285+ ...buildConnectionBody ( target , options ) ,
280286 }
281287}
282288
289+ function normalizeConnectionTarget (
290+ target : string | ConnectionTarget | undefined ,
291+ ) : Pick < ConnectionCreateParams , "mcpUrl" | "server" > {
292+ if ( ! target ) return { }
293+ if ( typeof target === "string" ) return { mcpUrl : target }
294+ return target
295+ }
296+
297+ function isHttpUrl ( value : string ) : boolean {
298+ return value . startsWith ( "http://" ) || value . startsWith ( "https://" )
299+ }
300+
283301function namespacePath ( namespace : string ) : string {
284302 return `/${ encodeURIComponent ( namespace ) } `
285303}
0 commit comments