list_functions
Retrieve all scalar and table-valued functions from a SQL Server database, with optional filtering by schema and function type for targeted results.
Instructions
Lists all functions (scalar and table-valued) in the database
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| schemaName | No | Schema name to filter functions | |
| functionType | No | Type of function to filter |
Implementation Reference
- src/tools/list-functions.ts:5-74 (handler)The core handler function that executes the 'list_functions' tool logic. It queries INFORMATION_SCHEMA.ROUTINES for functions, optionally filtering by schema name and function type (SCALAR/TABLE), returning a JSON list of functions.
export async function listFunctions( db: DatabaseConnection, schemaName?: string, functionType?: 'SCALAR' | 'TABLE' ): Promise<CallToolResult> { try { const pool = db.getPool() let query = ` SELECT ROUTINE_SCHEMA, ROUTINE_NAME, ROUTINE_TYPE, DATA_TYPE, CREATED, LAST_ALTERED, CASE WHEN DATA_TYPE = 'TABLE' THEN 'TABLE-VALUED' ELSE 'SCALAR' END AS FUNCTION_TYPE FROM INFORMATION_SCHEMA.ROUTINES WHERE ROUTINE_TYPE = 'FUNCTION' `.trim() const request = pool.request() if (schemaName) { query += ' AND ROUTINE_SCHEMA = @schemaName' request.input('schemaName', schemaName) } if (functionType) { if (functionType === 'SCALAR') { query += " AND DATA_TYPE != 'TABLE'" } else if (functionType === 'TABLE') { query += " AND DATA_TYPE = 'TABLE'" } } query += ' ORDER BY ROUTINE_SCHEMA, ROUTINE_NAME' const result = await request.query(query) return { content: [ { type: 'text', text: JSON.stringify( { functions: result.recordset, count: result.recordset.length, }, null, 2 ), }, ], } } catch (error) { return { content: [ { type: 'text', text: `Erro: ${error instanceof Error ? error.message : 'Erro desconhecido'}`, }, ], isError: true, } } } - src/schemas.ts:37-43 (schema)Zod schema defining the input validation for the 'list_functions' tool: optional schemaName (string) and optional functionType (enum: SCALAR or TABLE).
export const listFunctionsInput = z.object({ schemaName: z.string().optional().describe('Schema name to filter functions'), functionType: z .enum(['SCALAR', 'TABLE']) .optional() .describe('Type of function to filter'), }) - src/tools/index.ts:52-57 (registration)Registration of the 'list_functions' tool in the tool list with its name, description, and JSON-schema input definition.
{ name: 'list_functions', description: 'Lists all functions (scalar and table-valued) in the database', inputSchema: zodToJsonSchema(listFunctionsInput), }, - src/services/SqlServerMCPService.ts:103-106 (registration)Handler mapping that connects the 'list_functions' tool name to the actual listFunctions implementation, unpacks args (schemaName, functionType) and delegates.
handlers.set('list_functions', async (database, args) => { const { schemaName, functionType } = args as ListFunctionsInput return await listFunctions(database, schemaName, functionType) }) - src/tools/index.ts:21-21 (helper)Exports the listFunctions implementation from the tools index file, making it available for import by the service.
export { listFunctions } from './list-functions'