Skip to main content
Glama
cwilby

SQL Server MCP

by cwilby

query

Execute SQL statements directly on Microsoft SQL Server databases through a Model Context Protocol server designed for AI assistant integration.

Instructions

Query the database with a SQL statement

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
queryYesThe SQL query to execute

Implementation Reference

  • The handler function for the "query" MCP tool. It executes the provided SQL query using the database helper, handles empty results, and formats the output using toResult.
    async query({ query }: { query: string }) {
        const results = await database.query(query);
        if (!results.length) return this.toResult("No results found for the query.");
        return this.toResult(JSON.stringify(results));
    }
  • Input schema definition for the "query" tool using Zod, specifying a string parameter for the SQL query.
    { query: z.string().describe("The SQL query to execute") },
  • Registration of the "query" tool on the MCP server, including name, description, input schema, and binding to the handler method.
    server.tool(
        "query",
        "Query the database with a SQL statement",
        { query: z.string().describe("The SQL query to execute") },
        tools.query.bind(tools)
    );
  • Database query helper function called by the tool handler to execute SQL queries against the MSSQL database.
    export async function query(query: string, interpolations: any = null): Promise<mssql.IRecordSet<any>> {
    	let connection = null;
    	try {
    		connection = await connect();
    		const request = connection.request();
    		if (interpolations) {
    			Object.entries(interpolations).forEach(([key, value]) => request.input(key, value));
    		}
    		const { recordset } = await request.query(query);
    		return recordset;
    	} finally {
    		await connection?.close();
    	}
    }
  • Helper method from BaseTools used by the handler to format tool results as MCP CallToolResult.
    protected toResult(content: string): CallToolResult {
        return { content: [{ type: "text", text: content }] };
    }
Install Server

Other Tools

Related Tools

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/cwilby/mcp-node-mssql'

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