Skip to main content
Glama
vini-cius

SQL Server MCP Service

by vini-cius

get_function_schema

Retrieve the schema and parameter details for a specific SQL Server function to understand its structure and usage requirements.

Instructions

Gets the schema and parameters of a specific function

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
functionNameYesName of the function
schemaNameNoSchema name (default: dbo)dbo

Implementation Reference

  • Core handler function that executes SQL query to retrieve function schema, parameters, return type, and definition from SQL Server INFORMATION_SCHEMA.
    export async function getFunctionSchema(
      db: DatabaseConnection,
      functionName: string,
      schemaName: string = 'dbo'
    ): Promise<CallToolResult> {
      try {
        const pool = db.getPool()
    
        const query = `
          SELECT 
            p.PARAMETER_NAME,
            p.DATA_TYPE,
            p.PARAMETER_MODE,
            p.CHARACTER_MAXIMUM_LENGTH,
            p.NUMERIC_PRECISION,
            p.NUMERIC_SCALE,
            p.ORDINAL_POSITION,
            r.ROUTINE_DEFINITION,
            r.DATA_TYPE as RETURN_TYPE,
            CASE 
              WHEN r.DATA_TYPE = 'TABLE' THEN 'TABLE-VALUED'
              ELSE 'SCALAR'
            END AS FUNCTION_TYPE
          FROM INFORMATION_SCHEMA.PARAMETERS p
          INNER JOIN INFORMATION_SCHEMA.ROUTINES r 
            ON p.SPECIFIC_NAME = r.SPECIFIC_NAME
          WHERE r.ROUTINE_NAME = @functionName 
            AND r.ROUTINE_SCHEMA = @schemaName
            AND r.ROUTINE_TYPE = 'FUNCTION'
          ORDER BY p.ORDINAL_POSITION
        `.trim()
    
        const request = pool.request()
    
        request.input('functionName', functionName)
        request.input('schemaName', schemaName)
    
        const result = await request.query(query)
    
        return {
          content: [
            {
              type: 'text',
              text: JSON.stringify(
                {
                  function: `${schemaName}.${functionName}`,
                  functionType: result.recordset[0]?.FUNCTION_TYPE || 'UNKNOWN',
                  returnType: result.recordset[0]?.RETURN_TYPE || 'UNKNOWN',
                  parameters: result.recordset.map((row) => ({
                    name: row.PARAMETER_NAME,
                    dataType: row.DATA_TYPE,
                    mode: row.PARAMETER_MODE,
                    maxLength: row.CHARACTER_MAXIMUM_LENGTH,
                    precision: row.NUMERIC_PRECISION,
                    scale: row.NUMERIC_SCALE,
                    position: row.ORDINAL_POSITION,
                  })),
                  definition: result.recordset[0]?.ROUTINE_DEFINITION || null,
                },
                null,
                2
              ),
            },
          ],
        }
      } catch (error) {
        return {
          content: [
            {
              type: 'text',
              text: `Erro: ${error instanceof Error ? error.message : 'Erro desconhecido'}`,
            },
          ],
          isError: true,
        }
      }
    }
  • Zod input schema defining parameters for the get_function_schema tool: functionName (required) and schemaName (optional, defaults to 'dbo').
    export const getFunctionSchemaInput = z.object({
      functionName: z.string().describe('Name of the function'),
      schemaName: z.string().default('dbo').describe('Schema name (default: dbo)'),
    })
  • Registers the 'get_function_schema' tool handler in the MCP service's handler map, parsing args with the schema type and delegating to the core getFunctionSchema function.
    handlers.set('get_function_schema', async (database, args) => {
      const { functionName, schemaName } = args as GetFunctionSchemaInput
      return await getFunctionSchema(database, functionName, schemaName)
    })
  • Tool metadata registration in toolsList(): name, description, and JSON schema for input, used by MCP service for listTools response.
    {
      name: 'get_function_schema',
      description: 'Gets the schema and parameters of a specific function',
      inputSchema: zodToJsonSchema(getFunctionSchemaInput),
    },

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/vini-cius/mcp-sqlserver'

If you have feedback or need assistance with the MCP directory API, please join our Discord server