@marcelo-ochoa/server-postgres 1.0.7 → 1.0.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/server.js +53 -31
  2. package/package.json +1 -1
package/dist/server.js CHANGED
@@ -1,19 +1,13 @@
1
- import { Server } from "@modelcontextprotocol/sdk/server/index.js";
1
+ import { McpServer, ResourceTemplate } from "@modelcontextprotocol/sdk/server/mcp.js";
2
2
  import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
3
- import { CallToolRequestSchema, ListResourcesRequestSchema, ListResourceTemplatesRequestSchema, ListToolsRequestSchema, ReadResourceRequestSchema, } from "@modelcontextprotocol/sdk/types.js";
4
3
  import { z } from "zod";
5
4
  import { initializePool } from "./db.js";
6
5
  import { listResourcesHandler, readResourceHandler, callToolHandler } from "./handlers.js";
7
6
  import { tools } from "./tools.js";
8
- const server = new Server({
7
+ // Create server instance
8
+ const server = new McpServer({
9
9
  name: "postgres-server",
10
- version: "1.0.7",
11
- }, {
12
- capabilities: {
13
- resources: {},
14
- tools: {},
15
- prompts: {}, // Add this line to indicate support for prompts
16
- },
10
+ version: "1.0.8",
17
11
  });
18
12
  const prompts = [
19
13
  { name: "pg-query: Execute Query", description: "pg-query select * from test" },
@@ -22,30 +16,58 @@ const prompts = [
22
16
  { name: "pg-connect: Database Connection", description: "pg-connect to PostgreSQL using a connection string like host.docker.internal:5432/dbname with user name and password" },
23
17
  { name: "pg-awr: Performance Report", description: "pg-awr to generate a PostgreSQL performance report (requires pg_stat_statements extension)" }
24
18
  ];
25
- const PromptsListRequestSchema = z.object({
26
- method: z.literal("prompts/list"),
27
- params: z.object({}),
19
+ // Register Prompts
20
+ server.registerPrompt("postgres-prompts", {
21
+ description: "List available Postgres prompts"
22
+ }, async () => ({
23
+ messages: [
24
+ {
25
+ role: "assistant",
26
+ content: {
27
+ type: "text",
28
+ text: "Available Postgres prompts:\n" + prompts.map(p => `- ${p.name}: ${p.description}`).join("\n")
29
+ }
30
+ }
31
+ ]
32
+ }));
33
+ // Register Resource Templates
34
+ const resourceTemplate = new ResourceTemplate("postgres://{schema}/{table}/schema", {
35
+ list: async () => listResourcesHandler({})
28
36
  });
29
- server.setRequestHandler(PromptsListRequestSchema, async () => {
30
- return {
31
- prompts,
32
- };
37
+ server.registerResource("Table Schema", resourceTemplate, { description: "Schema information for a PostgreSQL database table including column names and data types" }, async (uri) => {
38
+ return readResourceHandler({ params: { uri: uri.toString() } });
33
39
  });
34
- server.setRequestHandler(ListResourceTemplatesRequestSchema, async () => {
35
- return {
36
- resourceTemplates: [
37
- {
38
- uriTemplate: "postgres://{schema}/{table_name}/schema",
39
- name: "Table Schema",
40
- description: "Schema information for a PostgreSQL database table including column names and data types",
41
- },
42
- ],
43
- };
40
+ // Register Tools
41
+ tools.forEach(tool => {
42
+ // Basic mapping of JSON schema to Zod for simple cases
43
+ let inputSchema = z.object({});
44
+ if (tool.inputSchema && tool.inputSchema.properties) {
45
+ const shape = {};
46
+ for (const [key, prop] of Object.entries(tool.inputSchema.properties)) {
47
+ let field = z.any();
48
+ if (prop.type === "string") {
49
+ field = z.string();
50
+ }
51
+ if (prop.description) {
52
+ field = field.describe(prop.description);
53
+ }
54
+ if (tool.inputSchema.required && !tool.inputSchema.required.includes(key)) {
55
+ field = field.optional();
56
+ }
57
+ else if (!tool.inputSchema.required) {
58
+ field = field.optional();
59
+ }
60
+ shape[key] = field;
61
+ }
62
+ inputSchema = z.object(shape);
63
+ }
64
+ server.registerTool(tool.name, {
65
+ description: tool.description,
66
+ inputSchema: inputSchema
67
+ }, async (args) => {
68
+ return callToolHandler({ params: { name: tool.name, arguments: args } });
69
+ });
44
70
  });
45
- server.setRequestHandler(ListResourcesRequestSchema, listResourcesHandler);
46
- server.setRequestHandler(ReadResourceRequestSchema, readResourceHandler);
47
- server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools }));
48
- server.setRequestHandler(CallToolRequestSchema, callToolHandler);
49
71
  export async function runServer() {
50
72
  const args = process.argv.slice(2);
51
73
  const databaseUrl = args[0];
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@marcelo-ochoa/server-postgres",
3
3
  "mcpName": "io.github.marcelo-ochoa/postgres",
4
- "version": "1.0.7",
4
+ "version": "1.0.8",
5
5
  "repository": {
6
6
  "type": "git",
7
7
  "url": "https://github.com/marcelo-ochoa/servers.git",