@@ -7,11 +7,11 @@ import { GRADIO_IMAGE_FILTER_FLAG } from '../shared/behavior-flags.js';
77import { logger } from './utils/logger.js' ;
88import { registerRemoteTools } from './gradio-endpoint-connector.js' ;
99import { extractAuthBouquetAndMix } from './utils/auth-utils.js' ;
10- import { parseGradioSpaceIds } from './utils/gradio-utils.js' ;
10+ import { parseGradioSpaceIds , shouldRegisterGradioFilesTool } from './utils/gradio-utils.js' ;
1111import { getGradioSpaces } from './utils/gradio-discovery.js' ;
1212import { repoExists } from '@huggingface/hub' ;
1313import type { GradioFilesParams } from '@llmindset/hf-mcp' ;
14- import { GRADIO_FILES_TOOL_CONFIG , GradioFilesTool } from '@llmindset/hf-mcp' ;
14+ import { GRADIO_FILES_TOOL_CONFIG , GradioFilesTool , DYNAMIC_SPACE_TOOL_ID } from '@llmindset/hf-mcp' ;
1515import { logSearchQuery } from './utils/query-logger.js' ;
1616import { z } from 'zod' ;
1717import type { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js' ;
@@ -159,6 +159,55 @@ export const createProxyServerFactory = (
159159 gradioParam : gradio ,
160160 } , 'Collected Gradio space names' ) ;
161161
162+ // Register gradio_files tool if eligible
163+ // This is needed when Gradio spaces are configured OR when dynamic_space is enabled
164+ if ( sessionInfo ?. isAuthenticated && userDetails ?. name && hfToken ) {
165+ const username = userDetails . name ;
166+ const token = hfToken ;
167+ const datasetExists = await repoExists ( {
168+ repo : { type : 'dataset' , name : `${ username } /gradio-files` } ,
169+ accessToken : token ,
170+ } ) ;
171+
172+ const shouldRegister = shouldRegisterGradioFilesTool ( {
173+ gradioSpaceCount : allSpaceNames . length ,
174+ builtInTools : settings ?. builtInTools ?? [ ] ,
175+ dynamicSpaceToolId : DYNAMIC_SPACE_TOOL_ID ,
176+ datasetExists,
177+ } ) ;
178+
179+ if ( shouldRegister ) {
180+ server . tool (
181+ GRADIO_FILES_TOOL_CONFIG . name ,
182+ GRADIO_FILES_TOOL_CONFIG . description ,
183+ GRADIO_FILES_TOOL_CONFIG . schema . shape ,
184+ GRADIO_FILES_TOOL_CONFIG . annotations ,
185+ async ( params : GradioFilesParams ) => {
186+ const tool = new GradioFilesTool ( token , username ) ;
187+ const markdown = await tool . generateDetailedMarkdown ( params . fileType ) ;
188+
189+ // Log the tool usage
190+ logSearchQuery (
191+ GRADIO_FILES_TOOL_CONFIG . name ,
192+ `${ username } /gradio-files` ,
193+ { fileType : params . fileType } ,
194+ {
195+ clientSessionId : sessionInfo ?. clientSessionId ,
196+ isAuthenticated : sessionInfo ?. isAuthenticated ?? true ,
197+ clientName : sessionInfo ?. clientInfo ?. name ,
198+ clientVersion : sessionInfo ?. clientInfo ?. version ,
199+ responseCharCount : markdown . length ,
200+ }
201+ ) ;
202+
203+ return {
204+ content : [ { type : 'text' , text : markdown } ] ,
205+ } ;
206+ }
207+ ) ;
208+ }
209+ }
210+
162211 if ( allSpaceNames . length === 0 ) {
163212 logger . debug ( 'No Gradio spaces configured, using local tools only' ) ;
164213 return result ;
@@ -212,64 +261,6 @@ export const createProxyServerFactory = (
212261 }
213262 }
214263
215- if ( sessionInfo ?. isAuthenticated && userDetails ?. name && hfToken ) {
216- const username = userDetails . name ; // Capture username for closure
217- const token = hfToken ; // Capture token for closure
218- const exists = await repoExists ( {
219- repo : { type : 'dataset' , name : `${ username } /gradio-files` } ,
220- } ) ;
221- if ( exists )
222- server . tool (
223- GRADIO_FILES_TOOL_CONFIG . name ,
224- GRADIO_FILES_TOOL_CONFIG . description ,
225- GRADIO_FILES_TOOL_CONFIG . schema . shape ,
226- GRADIO_FILES_TOOL_CONFIG . annotations ,
227- async ( params : GradioFilesParams ) => {
228- const tool = new GradioFilesTool ( token , username ) ;
229- const markdown = await tool . generateDetailedMarkdown ( params . fileType ) ;
230-
231- // Log the tool usage
232- logSearchQuery (
233- GRADIO_FILES_TOOL_CONFIG . name ,
234- `${ username } /gradio-files` ,
235- { fileType : params . fileType } ,
236- {
237- clientSessionId : sessionInfo ?. clientSessionId ,
238- isAuthenticated : sessionInfo ?. isAuthenticated ?? true ,
239- clientName : sessionInfo ?. clientInfo ?. name ,
240- clientVersion : sessionInfo ?. clientInfo ?. version ,
241- responseCharCount : markdown . length ,
242- }
243- ) ;
244-
245- return {
246- content : [ { type : 'text' , text : markdown } ] ,
247- } ;
248- }
249- ) ;
250- /* TODO -- reinstate once method handling is improved;
251- server.prompt(
252- GRADIO_FILES_PROMPT_CONFIG.name,
253- GRADIO_FILES_PROMPT_CONFIG.description,
254- GRADIO_FILES_PROMPT_CONFIG.schema.shape,
255- async () => {
256- return {
257- description: `Gradio Files summary for ${username}`,
258- messages: [
259- {
260- role: 'user' as const,
261- content: {
262- type: 'text' as const,
263- text: await new GradioFilesTool(token, username).generateDetailedMarkdown('all'),
264- },
265- },
266- ],
267- };
268- }
269- );
270- */
271- }
272-
273264 logger . debug ( 'Server ready with local and remote tools' ) ;
274265 return result ;
275266 } ;
0 commit comments