33import { Server } from '@modelcontextprotocol/sdk/server/index.js' ;
44import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js' ;
55import { Endpoint , endpoints , HandlerFunction , query } from './tools' ;
6- import { CallToolRequestSchema , ListToolsRequestSchema , Tool } from '@modelcontextprotocol/sdk/types.js' ;
6+ import {
7+ CallToolRequestSchema ,
8+ Implementation ,
9+ ListToolsRequestSchema ,
10+ Tool ,
11+ } from '@modelcontextprotocol/sdk/types.js' ;
712import { ClientOptions } from '@flowglad/node' ;
813import Flowglad from '@flowglad/node' ;
914import {
@@ -41,29 +46,29 @@ export const server = newMcpServer();
4146 */
4247export function initMcpServer ( params : {
4348 server : Server | McpServer ;
44- clientOptions : ClientOptions ;
45- mcpOptions : McpOptions ;
46- endpoints ?: { tool : Tool ; handler : HandlerFunction } [ ] ;
47- } ) {
48- const transformedEndpoints = selectTools ( endpoints , params . mcpOptions ) ;
49- const client = new Flowglad ( params . clientOptions ) ;
50- const capabilities = {
51- ...defaultClientCapabilities ,
52- ...( params . mcpOptions . client ? knownClients [ params . mcpOptions . client ] : params . mcpOptions . capabilities ) ,
53- } ;
54- init ( { server : params . server , client, endpoints : transformedEndpoints , capabilities } ) ;
55- }
56-
57- export function init ( params : {
58- server : Server | McpServer ;
59- client ?: Flowglad ;
60- endpoints ?: { tool : Tool ; handler : HandlerFunction } [ ] ;
61- capabilities ?: Partial < ClientCapabilities > ;
49+ clientOptions ?: ClientOptions ;
50+ mcpOptions ?: McpOptions ;
6251} ) {
6352 const server = params . server instanceof McpServer ? params . server . server : params . server ;
64- const providedEndpoints = params . endpoints || endpoints ;
65-
66- const endpointMap = Object . fromEntries ( providedEndpoints . map ( ( endpoint ) => [ endpoint . tool . name , endpoint ] ) ) ;
53+ const mcpOptions = params . mcpOptions ?? { } ;
54+
55+ let providedEndpoints : Endpoint [ ] | null = null ;
56+ let endpointMap : Record < string , Endpoint > | null = null ;
57+
58+ const initTools = ( implementation ?: Implementation ) => {
59+ if ( implementation && ( ! mcpOptions . client || mcpOptions . client === 'infer' ) ) {
60+ mcpOptions . client =
61+ implementation . name . toLowerCase ( ) . includes ( 'claude' ) ? 'claude'
62+ : implementation . name . toLowerCase ( ) . includes ( 'cursor' ) ? 'cursor'
63+ : undefined ;
64+ mcpOptions . capabilities = {
65+ ...( mcpOptions . client && knownClients [ mcpOptions . client ] ) ,
66+ ...mcpOptions . capabilities ,
67+ } ;
68+ }
69+ providedEndpoints = selectTools ( endpoints , mcpOptions ) ;
70+ endpointMap = Object . fromEntries ( providedEndpoints . map ( ( endpoint ) => [ endpoint . tool . name , endpoint ] ) ) ;
71+ } ;
6772
6873 const logAtLevel =
6974 ( level : 'debug' | 'info' | 'warning' | 'error' ) =>
@@ -80,51 +85,63 @@ export function init(params: {
8085 error : logAtLevel ( 'error' ) ,
8186 } ;
8287
83- const client =
84- params . client || new Flowglad ( { defaultHeaders : { 'X-Stainless-MCP' : 'true' } , logger : logger } ) ;
88+ const client = new Flowglad ( {
89+ logger,
90+ ...params . clientOptions ,
91+ defaultHeaders : {
92+ ...params . clientOptions ?. defaultHeaders ,
93+ 'X-Stainless-MCP' : 'true' ,
94+ } ,
95+ } ) ;
8596
8697 server . setRequestHandler ( ListToolsRequestSchema , async ( ) => {
98+ if ( providedEndpoints === null ) {
99+ initTools ( server . getClientVersion ( ) ) ;
100+ }
87101 return {
88- tools : providedEndpoints . map ( ( endpoint ) => endpoint . tool ) ,
102+ tools : providedEndpoints ! . map ( ( endpoint ) => endpoint . tool ) ,
89103 } ;
90104 } ) ;
91105
92106 server . setRequestHandler ( CallToolRequestSchema , async ( request ) => {
107+ if ( endpointMap === null ) {
108+ initTools ( server . getClientVersion ( ) ) ;
109+ }
93110 const { name, arguments : args } = request . params ;
94- const endpoint = endpointMap [ name ] ;
111+ const endpoint = endpointMap ! [ name ] ;
95112 if ( ! endpoint ) {
96113 throw new Error ( `Unknown tool: ${ name } ` ) ;
97114 }
98115
99- return executeHandler ( endpoint . tool , endpoint . handler , client , args , params . capabilities ) ;
116+ return executeHandler ( endpoint . tool , endpoint . handler , client , args , mcpOptions . capabilities ) ;
100117 } ) ;
101118}
102119
103120/**
104121 * Selects the tools to include in the MCP Server based on the provided options.
105122 */
106- export function selectTools ( endpoints : Endpoint [ ] , options : McpOptions ) : Endpoint [ ] {
107- const filteredEndpoints = query ( options . filters , endpoints ) ;
123+ export function selectTools ( endpoints : Endpoint [ ] , options ? : McpOptions ) : Endpoint [ ] {
124+ const filteredEndpoints = query ( options ? .filters ?? [ ] , endpoints ) ;
108125
109126 let includedTools = filteredEndpoints ;
110127
111128 if ( includedTools . length > 0 ) {
112- if ( options . includeDynamicTools ) {
129+ if ( options ? .includeDynamicTools ) {
113130 includedTools = dynamicTools ( includedTools ) ;
114131 }
115132 } else {
116- if ( options . includeAllTools ) {
133+ if ( options ? .includeAllTools ) {
117134 includedTools = endpoints ;
118- } else if ( options . includeDynamicTools ) {
135+ } else if ( options ? .includeDynamicTools ) {
119136 includedTools = dynamicTools ( endpoints ) ;
120- } else if ( options . includeCodeTools ) {
137+ } else if ( options ? .includeCodeTools ) {
121138 includedTools = [ codeTool ( ) ] ;
122139 } else {
123140 includedTools = endpoints ;
124141 }
125142 }
126143
127- const capabilities = { ...defaultClientCapabilities , ...options . capabilities } ;
144+ const capabilities = { ...defaultClientCapabilities , ...options ? .capabilities } ;
128145 return applyCompatibilityTransformations ( includedTools , capabilities ) ;
129146}
130147
0 commit comments