get_view_definition
Retrieve the SQL CREATE VIEW statement for a specified view in the database, using schema and view name.
Instructions
Return the SQL definition of a view.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| schema | Yes | ||
| view | Yes |
Implementation Reference
- src/index.ts:119-131 (registration)Registration of the 'get_view_definition' tool on the MCP server, defining its schema (schema and view string params) and handler which calls db.getViewDefinition().
server.tool( 'get_view_definition', 'Return the SQL definition of a view.', { schema: z.string(), view: z.string(), }, async ({ schema, view }) => runTool(async () => { const def = await db.getViewDefinition(schema, view); return def ?? `[NOT_FOUND] View ${schema}.${view} not found.`; }) ); - src/index.ts:126-130 (handler)Handler function that invokes db.getViewDefinition(schema, view) and returns the definition or a [NOT_FOUND] fallback.
async ({ schema, view }) => runTool(async () => { const def = await db.getViewDefinition(schema, view); return def ?? `[NOT_FOUND] View ${schema}.${view} not found.`; }) - src/index.ts:122-125 (schema)Zod schema defining the two required input parameters: schema (string) and view (string).
{ schema: z.string(), view: z.string(), }, - src/db.ts:220-232 (helper)The DbManager.getViewDefinition() method that queries sys.sql_modules joined with sys.views and sys.schemas to retrieve the SQL definition of a view.
async getViewDefinition(schema: string, view: string): Promise<string | null> { const r = await (await this.getPool()) .request() .input('schema', sql.NVarChar, schema) .input('view', sql.NVarChar, view).query(` SELECT m.definition FROM sys.sql_modules m INNER JOIN sys.views v ON m.object_id = v.object_id INNER JOIN sys.schemas s ON v.schema_id = s.schema_id WHERE s.name = @schema AND v.name = @view `); return r.recordset[0]?.definition ?? null; }