@mcp-ts/sdk 1.3.6 → 1.3.9
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.
- package/LICENSE +21 -21
- package/README.md +398 -404
- package/dist/adapters/agui-adapter.d.mts +1 -1
- package/dist/adapters/agui-adapter.d.ts +1 -1
- package/dist/adapters/agui-adapter.js +2 -2
- package/dist/adapters/agui-adapter.js.map +1 -1
- package/dist/adapters/agui-adapter.mjs +2 -2
- package/dist/adapters/agui-adapter.mjs.map +1 -1
- package/dist/adapters/agui-middleware.d.mts +1 -1
- package/dist/adapters/agui-middleware.d.ts +1 -1
- package/dist/adapters/agui-middleware.js.map +1 -1
- package/dist/adapters/agui-middleware.mjs.map +1 -1
- package/dist/adapters/ai-adapter.d.mts +1 -1
- package/dist/adapters/ai-adapter.d.ts +1 -1
- package/dist/adapters/ai-adapter.js +1 -1
- package/dist/adapters/ai-adapter.js.map +1 -1
- package/dist/adapters/ai-adapter.mjs +1 -1
- package/dist/adapters/ai-adapter.mjs.map +1 -1
- package/dist/adapters/langchain-adapter.d.mts +1 -1
- package/dist/adapters/langchain-adapter.d.ts +1 -1
- package/dist/adapters/langchain-adapter.js +1 -1
- package/dist/adapters/langchain-adapter.js.map +1 -1
- package/dist/adapters/langchain-adapter.mjs +1 -1
- package/dist/adapters/langchain-adapter.mjs.map +1 -1
- package/dist/adapters/mastra-adapter.d.mts +1 -1
- package/dist/adapters/mastra-adapter.d.ts +1 -1
- package/dist/adapters/mastra-adapter.js +1 -1
- package/dist/adapters/mastra-adapter.js.map +1 -1
- package/dist/adapters/mastra-adapter.mjs +1 -1
- package/dist/adapters/mastra-adapter.mjs.map +1 -1
- package/dist/bin/mcp-ts.js +0 -0
- package/dist/bin/mcp-ts.js.map +1 -1
- package/dist/bin/mcp-ts.mjs +0 -0
- package/dist/bin/mcp-ts.mjs.map +1 -1
- package/dist/client/index.js.map +1 -1
- package/dist/client/index.mjs.map +1 -1
- package/dist/client/react.d.mts +2 -2
- package/dist/client/react.d.ts +2 -2
- package/dist/client/react.js +25 -2
- package/dist/client/react.js.map +1 -1
- package/dist/client/react.mjs +26 -3
- package/dist/client/react.mjs.map +1 -1
- package/dist/client/vue.js.map +1 -1
- package/dist/client/vue.mjs.map +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +134 -71
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +134 -71
- package/dist/index.mjs.map +1 -1
- package/dist/{multi-session-client-BYLarghq.d.ts → multi-session-client-CHE8QpVE.d.ts} +75 -5
- package/dist/{multi-session-client-CzhMkE0k.d.mts → multi-session-client-CQsRbxYI.d.mts} +75 -5
- package/dist/server/index.d.mts +1 -1
- package/dist/server/index.d.ts +1 -1
- package/dist/server/index.js +134 -71
- package/dist/server/index.js.map +1 -1
- package/dist/server/index.mjs +134 -71
- package/dist/server/index.mjs.map +1 -1
- package/dist/shared/index.js +10 -2
- package/dist/shared/index.js.map +1 -1
- package/dist/shared/index.mjs +10 -2
- package/dist/shared/index.mjs.map +1 -1
- package/package.json +185 -185
- package/src/adapters/agui-adapter.ts +222 -222
- package/src/adapters/agui-middleware.ts +382 -382
- package/src/adapters/ai-adapter.ts +115 -115
- package/src/adapters/langchain-adapter.ts +127 -127
- package/src/adapters/mastra-adapter.ts +126 -126
- package/src/bin/mcp-ts.ts +102 -102
- package/src/client/core/app-host.ts +417 -417
- package/src/client/core/sse-client.ts +371 -371
- package/src/client/core/types.ts +31 -31
- package/src/client/index.ts +27 -27
- package/src/client/react/index.ts +16 -16
- package/src/client/react/use-app-host.ts +73 -73
- package/src/client/react/use-mcp-apps.tsx +247 -214
- package/src/client/react/use-mcp.ts +641 -641
- package/src/client/vue/index.ts +10 -10
- package/src/client/vue/use-mcp.ts +617 -617
- package/src/index.ts +11 -11
- package/src/server/handlers/nextjs-handler.ts +204 -204
- package/src/server/handlers/sse-handler.ts +631 -631
- package/src/server/index.ts +57 -57
- package/src/server/mcp/multi-session-client.ts +228 -132
- package/src/server/mcp/oauth-client.ts +1188 -1188
- package/src/server/mcp/storage-oauth-provider.ts +272 -272
- package/src/server/storage/file-backend.ts +157 -170
- package/src/server/storage/index.ts +176 -175
- package/src/server/storage/memory-backend.ts +123 -136
- package/src/server/storage/redis-backend.ts +276 -289
- package/src/server/storage/redis.ts +160 -160
- package/src/server/storage/sqlite-backend.ts +182 -186
- package/src/server/storage/supabase-backend.ts +228 -227
- package/src/server/storage/types.ts +116 -116
- package/src/shared/constants.ts +29 -29
- package/src/shared/errors.ts +133 -133
- package/src/shared/event-routing.ts +28 -28
- package/src/shared/events.ts +180 -180
- package/src/shared/index.ts +75 -75
- package/src/shared/tool-utils.ts +61 -61
- package/src/shared/types.ts +282 -282
- package/src/shared/utils.ts +38 -16
- package/supabase/migrations/20260330195700_install_mcp_sessions.sql +84 -84
|
@@ -1,126 +1,126 @@
|
|
|
1
|
-
import { MCPClient } from '../server/mcp/oauth-client';
|
|
2
|
-
import { MultiSessionClient } from '../server/mcp/multi-session-client';
|
|
3
|
-
import type { z } from 'zod';
|
|
4
|
-
|
|
5
|
-
export interface MastraAdapterOptions {
|
|
6
|
-
/**
|
|
7
|
-
* Prefix for tool names to avoid collision with other tools.
|
|
8
|
-
* Defaults to the client's serverId.
|
|
9
|
-
*/
|
|
10
|
-
prefix?: string;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Interface definition for a Mastra tool since we might not have the SDK installed.
|
|
15
|
-
* Based on Mastra documentation.
|
|
16
|
-
*/
|
|
17
|
-
export interface MastraTool {
|
|
18
|
-
id: string;
|
|
19
|
-
description: string;
|
|
20
|
-
inputSchema: z.ZodType<any>;
|
|
21
|
-
execute: (args: any) => Promise<any>;
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* Adapter to use MCP tools within Mastra agents.
|
|
26
|
-
*/
|
|
27
|
-
export class MastraAdapter {
|
|
28
|
-
private z: typeof z | undefined;
|
|
29
|
-
|
|
30
|
-
constructor(
|
|
31
|
-
private client: MCPClient | MultiSessionClient,
|
|
32
|
-
private options: MastraAdapterOptions = {}
|
|
33
|
-
) { }
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Lazy-loads Zod dependency
|
|
37
|
-
*/
|
|
38
|
-
private async ensureZod() {
|
|
39
|
-
if (!this.z) {
|
|
40
|
-
try {
|
|
41
|
-
const zod = await import('zod');
|
|
42
|
-
this.z = zod.z;
|
|
43
|
-
} catch (error) {
|
|
44
|
-
throw new Error(
|
|
45
|
-
'zod is not installed. Install with:\n' +
|
|
46
|
-
' npm install zod'
|
|
47
|
-
);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
private async transformTools(client: MCPClient): Promise<Record<string, MastraTool>> {
|
|
55
|
-
if (!client.isConnected()) {
|
|
56
|
-
return {};
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
await this.ensureZod();
|
|
60
|
-
|
|
61
|
-
const result = await client.listTools();
|
|
62
|
-
const prefix = this.options.prefix ?? client.getServerId() ?? 'mcp';
|
|
63
|
-
const tools: Record<string, MastraTool> = {};
|
|
64
|
-
|
|
65
|
-
for (const tool of result.tools) {
|
|
66
|
-
const toolName = `${prefix}_${tool.name}`;
|
|
67
|
-
|
|
68
|
-
// In a real implementation, you would use a library like 'json-schema-to-zod'
|
|
69
|
-
const schema = this.jsonSchemaToZod(tool.inputSchema);
|
|
70
|
-
|
|
71
|
-
tools[toolName] = {
|
|
72
|
-
id: toolName,
|
|
73
|
-
description: tool.description || `Tool ${tool.name}`,
|
|
74
|
-
inputSchema: schema,
|
|
75
|
-
execute: async (args: any) => {
|
|
76
|
-
return await client.callTool(tool.name, args);
|
|
77
|
-
},
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
return tools;
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
private jsonSchemaToZod(schema: any): z.ZodType<any> {
|
|
85
|
-
try {
|
|
86
|
-
const { parseSchema } = require('json-schema-to-zod');
|
|
87
|
-
const zodSchemaString = parseSchema(schema);
|
|
88
|
-
// eslint-disable-next-line
|
|
89
|
-
return new Function('z', 'return ' + zodSchemaString)(this.z);
|
|
90
|
-
} catch (error) {
|
|
91
|
-
// Fallback: Accept any object if conversion fails
|
|
92
|
-
console.warn('[MastraAdapter] Failed to convert JSON Schema to Zod, using fallback:', error);
|
|
93
|
-
return this.z!.record(this.z!.any()).optional().describe("Dynamic Input");
|
|
94
|
-
}
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Fetches tools from the MCP server and converts them to Mastra tools.
|
|
99
|
-
*/
|
|
100
|
-
async getTools(): Promise<Record<string, MastraTool>> {
|
|
101
|
-
// Use duck typing instead of instanceof to handle module bundling issues
|
|
102
|
-
const isMultiSession = typeof (this.client as any).getClients === 'function';
|
|
103
|
-
const clients = isMultiSession
|
|
104
|
-
? (this.client as MultiSessionClient).getClients()
|
|
105
|
-
: [this.client as MCPClient];
|
|
106
|
-
|
|
107
|
-
const results = await Promise.all(
|
|
108
|
-
clients.map(async (client) => {
|
|
109
|
-
try {
|
|
110
|
-
return await this.transformTools(client);
|
|
111
|
-
} catch (error) {
|
|
112
|
-
console.error(`[MastraAdapter] Failed to fetch tools from ${client.getServerId()}:`, error);
|
|
113
|
-
return {};
|
|
114
|
-
}
|
|
115
|
-
})
|
|
116
|
-
);
|
|
117
|
-
return results.reduce((acc, tools) => ({ ...acc, ...tools }), {});
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
/**
|
|
121
|
-
* Convenience static method to fetch tools in a single line.
|
|
122
|
-
*/
|
|
123
|
-
static async getTools(client: MCPClient | MultiSessionClient, options: MastraAdapterOptions = {}): Promise<Record<string, MastraTool>> {
|
|
124
|
-
return new MastraAdapter(client, options).getTools();
|
|
125
|
-
}
|
|
126
|
-
}
|
|
1
|
+
import { MCPClient } from '../server/mcp/oauth-client';
|
|
2
|
+
import { MultiSessionClient } from '../server/mcp/multi-session-client';
|
|
3
|
+
import type { z } from 'zod';
|
|
4
|
+
|
|
5
|
+
export interface MastraAdapterOptions {
|
|
6
|
+
/**
|
|
7
|
+
* Prefix for tool names to avoid collision with other tools.
|
|
8
|
+
* Defaults to the client's serverId.
|
|
9
|
+
*/
|
|
10
|
+
prefix?: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Interface definition for a Mastra tool since we might not have the SDK installed.
|
|
15
|
+
* Based on Mastra documentation.
|
|
16
|
+
*/
|
|
17
|
+
export interface MastraTool {
|
|
18
|
+
id: string;
|
|
19
|
+
description: string;
|
|
20
|
+
inputSchema: z.ZodType<any>;
|
|
21
|
+
execute: (args: any) => Promise<any>;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Adapter to use MCP tools within Mastra agents.
|
|
26
|
+
*/
|
|
27
|
+
export class MastraAdapter {
|
|
28
|
+
private z: typeof z | undefined;
|
|
29
|
+
|
|
30
|
+
constructor(
|
|
31
|
+
private client: MCPClient | MultiSessionClient,
|
|
32
|
+
private options: MastraAdapterOptions = {}
|
|
33
|
+
) { }
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Lazy-loads Zod dependency
|
|
37
|
+
*/
|
|
38
|
+
private async ensureZod() {
|
|
39
|
+
if (!this.z) {
|
|
40
|
+
try {
|
|
41
|
+
const zod = await import('zod');
|
|
42
|
+
this.z = zod.z;
|
|
43
|
+
} catch (error) {
|
|
44
|
+
throw new Error(
|
|
45
|
+
'zod is not installed. Install with:\n' +
|
|
46
|
+
' npm install zod'
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
private async transformTools(client: MCPClient): Promise<Record<string, MastraTool>> {
|
|
55
|
+
if (!client.isConnected()) {
|
|
56
|
+
return {};
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
await this.ensureZod();
|
|
60
|
+
|
|
61
|
+
const result = await client.listTools();
|
|
62
|
+
const prefix = this.options.prefix ?? client.getServerId()?.replace(/-/g, '').substring(0, 8) ?? 'mcp';
|
|
63
|
+
const tools: Record<string, MastraTool> = {};
|
|
64
|
+
|
|
65
|
+
for (const tool of result.tools) {
|
|
66
|
+
const toolName = `${prefix}_${tool.name}`;
|
|
67
|
+
|
|
68
|
+
// In a real implementation, you would use a library like 'json-schema-to-zod'
|
|
69
|
+
const schema = this.jsonSchemaToZod(tool.inputSchema);
|
|
70
|
+
|
|
71
|
+
tools[toolName] = {
|
|
72
|
+
id: toolName,
|
|
73
|
+
description: tool.description || `Tool ${tool.name}`,
|
|
74
|
+
inputSchema: schema,
|
|
75
|
+
execute: async (args: any) => {
|
|
76
|
+
return await client.callTool(tool.name, args);
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
return tools;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
private jsonSchemaToZod(schema: any): z.ZodType<any> {
|
|
85
|
+
try {
|
|
86
|
+
const { parseSchema } = require('json-schema-to-zod');
|
|
87
|
+
const zodSchemaString = parseSchema(schema);
|
|
88
|
+
// eslint-disable-next-line
|
|
89
|
+
return new Function('z', 'return ' + zodSchemaString)(this.z);
|
|
90
|
+
} catch (error) {
|
|
91
|
+
// Fallback: Accept any object if conversion fails
|
|
92
|
+
console.warn('[MastraAdapter] Failed to convert JSON Schema to Zod, using fallback:', error);
|
|
93
|
+
return this.z!.record(this.z!.any()).optional().describe("Dynamic Input");
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Fetches tools from the MCP server and converts them to Mastra tools.
|
|
99
|
+
*/
|
|
100
|
+
async getTools(): Promise<Record<string, MastraTool>> {
|
|
101
|
+
// Use duck typing instead of instanceof to handle module bundling issues
|
|
102
|
+
const isMultiSession = typeof (this.client as any).getClients === 'function';
|
|
103
|
+
const clients = isMultiSession
|
|
104
|
+
? (this.client as MultiSessionClient).getClients()
|
|
105
|
+
: [this.client as MCPClient];
|
|
106
|
+
|
|
107
|
+
const results = await Promise.all(
|
|
108
|
+
clients.map(async (client) => {
|
|
109
|
+
try {
|
|
110
|
+
return await this.transformTools(client);
|
|
111
|
+
} catch (error) {
|
|
112
|
+
console.error(`[MastraAdapter] Failed to fetch tools from ${client.getServerId()}:`, error);
|
|
113
|
+
return {};
|
|
114
|
+
}
|
|
115
|
+
})
|
|
116
|
+
);
|
|
117
|
+
return results.reduce((acc, tools) => ({ ...acc, ...tools }), {});
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Convenience static method to fetch tools in a single line.
|
|
122
|
+
*/
|
|
123
|
+
static async getTools(client: MCPClient | MultiSessionClient, options: MastraAdapterOptions = {}): Promise<Record<string, MastraTool>> {
|
|
124
|
+
return new MastraAdapter(client, options).getTools();
|
|
125
|
+
}
|
|
126
|
+
}
|
package/src/bin/mcp-ts.ts
CHANGED
|
@@ -1,102 +1,102 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
import * as fs from 'fs';
|
|
3
|
-
import * as path from 'path';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* MCP-TS CLI Utility
|
|
7
|
-
*
|
|
8
|
-
* Provides helper commands for users of the @mcp-ts/sdk library.
|
|
9
|
-
*/
|
|
10
|
-
async function run() {
|
|
11
|
-
const args = process.argv.slice(2);
|
|
12
|
-
const command = args[0];
|
|
13
|
-
|
|
14
|
-
if (command === 'supabase-init') {
|
|
15
|
-
await initSupabase();
|
|
16
|
-
} else {
|
|
17
|
-
showHelp();
|
|
18
|
-
}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
function showHelp() {
|
|
22
|
-
console.log(`
|
|
23
|
-
🚀 MCP-TS CLI Utility
|
|
24
|
-
Usage: npx mcp-ts <command>
|
|
25
|
-
|
|
26
|
-
Commands:
|
|
27
|
-
supabase-init Initialize Supabase migrations in your project
|
|
28
|
-
`);
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
async function initSupabase() {
|
|
32
|
-
console.log('🚀 Initializing Supabase storage for MCP-TS...');
|
|
33
|
-
|
|
34
|
-
// When running from dist/bin/mcp-ts.js (compiled), __dirname is dist/bin.
|
|
35
|
-
// The supabase/ migrations are at the root of the package.
|
|
36
|
-
// We need to look up two levels to find 'supabase' folder in the package.
|
|
37
|
-
const pkgRoot = path.resolve(__dirname, '../..');
|
|
38
|
-
const sourceDir = path.join(pkgRoot, 'supabase', 'migrations');
|
|
39
|
-
|
|
40
|
-
if (!fs.existsSync(sourceDir)) {
|
|
41
|
-
console.error(`❌ Error: Could not find migration files in package at: ${sourceDir}`);
|
|
42
|
-
console.log('Please ensure you are running this from a project where @mcp-ts/sdk is installed.');
|
|
43
|
-
process.exit(1);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
const targetDir = path.join(process.cwd(), 'supabase', 'migrations');
|
|
47
|
-
|
|
48
|
-
try {
|
|
49
|
-
if (!fs.existsSync(targetDir)) {
|
|
50
|
-
fs.mkdirSync(targetDir, { recursive: true });
|
|
51
|
-
console.log(`📁 Created directory: ${targetDir}`);
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
const files = fs.readdirSync(sourceDir);
|
|
55
|
-
let copiedCount = 0;
|
|
56
|
-
|
|
57
|
-
for (const file of files) {
|
|
58
|
-
if (file.endsWith('.sql')) {
|
|
59
|
-
const srcPath = path.join(sourceDir, file);
|
|
60
|
-
const destPath = path.join(targetDir, file);
|
|
61
|
-
|
|
62
|
-
if (fs.existsSync(destPath)) {
|
|
63
|
-
console.log(`⏭️ Skipping existing migration: ${file}`);
|
|
64
|
-
continue;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
fs.copyFileSync(srcPath, destPath);
|
|
68
|
-
console.log(`✅ Copied: ${file}`);
|
|
69
|
-
copiedCount++;
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
if (copiedCount > 0) {
|
|
74
|
-
console.log('\n✨ Database migrations successfully initialized!');
|
|
75
|
-
console.log('\nNext steps:');
|
|
76
|
-
console.log('1. Link your Supabase project:');
|
|
77
|
-
console.log(' npx supabase link --project-ref <your-project-id>');
|
|
78
|
-
console.log('\n2. Push the migrations to your remote database:');
|
|
79
|
-
console.log(' npx supabase db push');
|
|
80
|
-
console.log('\n3. Add your Supabase credentials to .env:');
|
|
81
|
-
console.log(' SUPABASE_URL=https://<your-project-id>.supabase.co');
|
|
82
|
-
console.log(' SUPABASE_SERVICE_ROLE_KEY=<your-service-role-key>');
|
|
83
|
-
console.log('\n⚠️ Important: Use the service_role key (not the anon key) for server-side storage.');
|
|
84
|
-
console.log(' The service_role key bypasses RLS policies and is required for mcp-ts to work correctly.');
|
|
85
|
-
console.log(' Find it in: Supabase Dashboard -> Project Settings -> API -> service_role');
|
|
86
|
-
} else if (files.length > 0) {
|
|
87
|
-
console.log('\n👍 All migration files are already present in your project.');
|
|
88
|
-
console.log(' Ensure SUPABASE_SERVICE_ROLE_KEY (not SUPABASE_ANON_KEY) is set in your .env');
|
|
89
|
-
} else {
|
|
90
|
-
console.log('⚠️ No migration files found to copy.');
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
} catch (error: any) {
|
|
94
|
-
console.error(`❌ Error initializing Supabase: ${error.message}`);
|
|
95
|
-
process.exit(1);
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
run().catch(err => {
|
|
100
|
-
console.error(err);
|
|
101
|
-
process.exit(1);
|
|
102
|
-
});
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import * as fs from 'fs';
|
|
3
|
+
import * as path from 'path';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* MCP-TS CLI Utility
|
|
7
|
+
*
|
|
8
|
+
* Provides helper commands for users of the @mcp-ts/sdk library.
|
|
9
|
+
*/
|
|
10
|
+
async function run() {
|
|
11
|
+
const args = process.argv.slice(2);
|
|
12
|
+
const command = args[0];
|
|
13
|
+
|
|
14
|
+
if (command === 'supabase-init') {
|
|
15
|
+
await initSupabase();
|
|
16
|
+
} else {
|
|
17
|
+
showHelp();
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function showHelp() {
|
|
22
|
+
console.log(`
|
|
23
|
+
🚀 MCP-TS CLI Utility
|
|
24
|
+
Usage: npx mcp-ts <command>
|
|
25
|
+
|
|
26
|
+
Commands:
|
|
27
|
+
supabase-init Initialize Supabase migrations in your project
|
|
28
|
+
`);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async function initSupabase() {
|
|
32
|
+
console.log('🚀 Initializing Supabase storage for MCP-TS...');
|
|
33
|
+
|
|
34
|
+
// When running from dist/bin/mcp-ts.js (compiled), __dirname is dist/bin.
|
|
35
|
+
// The supabase/ migrations are at the root of the package.
|
|
36
|
+
// We need to look up two levels to find 'supabase' folder in the package.
|
|
37
|
+
const pkgRoot = path.resolve(__dirname, '../..');
|
|
38
|
+
const sourceDir = path.join(pkgRoot, 'supabase', 'migrations');
|
|
39
|
+
|
|
40
|
+
if (!fs.existsSync(sourceDir)) {
|
|
41
|
+
console.error(`❌ Error: Could not find migration files in package at: ${sourceDir}`);
|
|
42
|
+
console.log('Please ensure you are running this from a project where @mcp-ts/sdk is installed.');
|
|
43
|
+
process.exit(1);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
const targetDir = path.join(process.cwd(), 'supabase', 'migrations');
|
|
47
|
+
|
|
48
|
+
try {
|
|
49
|
+
if (!fs.existsSync(targetDir)) {
|
|
50
|
+
fs.mkdirSync(targetDir, { recursive: true });
|
|
51
|
+
console.log(`📁 Created directory: ${targetDir}`);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const files = fs.readdirSync(sourceDir);
|
|
55
|
+
let copiedCount = 0;
|
|
56
|
+
|
|
57
|
+
for (const file of files) {
|
|
58
|
+
if (file.endsWith('.sql')) {
|
|
59
|
+
const srcPath = path.join(sourceDir, file);
|
|
60
|
+
const destPath = path.join(targetDir, file);
|
|
61
|
+
|
|
62
|
+
if (fs.existsSync(destPath)) {
|
|
63
|
+
console.log(`⏭️ Skipping existing migration: ${file}`);
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
fs.copyFileSync(srcPath, destPath);
|
|
68
|
+
console.log(`✅ Copied: ${file}`);
|
|
69
|
+
copiedCount++;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (copiedCount > 0) {
|
|
74
|
+
console.log('\n✨ Database migrations successfully initialized!');
|
|
75
|
+
console.log('\nNext steps:');
|
|
76
|
+
console.log('1. Link your Supabase project:');
|
|
77
|
+
console.log(' npx supabase link --project-ref <your-project-id>');
|
|
78
|
+
console.log('\n2. Push the migrations to your remote database:');
|
|
79
|
+
console.log(' npx supabase db push');
|
|
80
|
+
console.log('\n3. Add your Supabase credentials to .env:');
|
|
81
|
+
console.log(' SUPABASE_URL=https://<your-project-id>.supabase.co');
|
|
82
|
+
console.log(' SUPABASE_SERVICE_ROLE_KEY=<your-service-role-key>');
|
|
83
|
+
console.log('\n⚠️ Important: Use the service_role key (not the anon key) for server-side storage.');
|
|
84
|
+
console.log(' The service_role key bypasses RLS policies and is required for mcp-ts to work correctly.');
|
|
85
|
+
console.log(' Find it in: Supabase Dashboard -> Project Settings -> API -> service_role');
|
|
86
|
+
} else if (files.length > 0) {
|
|
87
|
+
console.log('\n👍 All migration files are already present in your project.');
|
|
88
|
+
console.log(' Ensure SUPABASE_SERVICE_ROLE_KEY (not SUPABASE_ANON_KEY) is set in your .env');
|
|
89
|
+
} else {
|
|
90
|
+
console.log('⚠️ No migration files found to copy.');
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
} catch (error: any) {
|
|
94
|
+
console.error(`❌ Error initializing Supabase: ${error.message}`);
|
|
95
|
+
process.exit(1);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
run().catch(err => {
|
|
100
|
+
console.error(err);
|
|
101
|
+
process.exit(1);
|
|
102
|
+
});
|