@objectstack/plugin-mcp-server 4.0.2
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/.turbo/turbo-build.log +22 -0
- package/LICENSE +202 -0
- package/dist/index.cjs +474 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +178 -0
- package/dist/index.d.ts +178 -0
- package/dist/index.js +446 -0
- package/dist/index.js.map +1 -0
- package/package.json +31 -0
- package/src/__tests__/mcp-server-runtime.test.ts +279 -0
- package/src/__tests__/plugin.test.ts +239 -0
- package/src/index.ts +15 -0
- package/src/mcp-server-runtime.ts +495 -0
- package/src/plugin.ts +139 -0
- package/src/types.ts +24 -0
- package/tsconfig.json +18 -0
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { Plugin, PluginContext } from '@objectstack/core';
|
|
2
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
3
|
+
import { AIToolDefinition, ToolCallPart, ToolResultPart, Logger, IMetadataService, IDataEngine } from '@objectstack/spec/contracts';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Configuration options for the MCPServerPlugin.
|
|
7
|
+
*/
|
|
8
|
+
interface MCPServerPluginOptions {
|
|
9
|
+
/** Override MCP server name. Defaults to 'objectstack'. */
|
|
10
|
+
name?: string;
|
|
11
|
+
/** Override MCP server version. Defaults to package version. */
|
|
12
|
+
version?: string;
|
|
13
|
+
/** Transport mode: 'stdio' (default). */
|
|
14
|
+
transport?: 'stdio' | 'http';
|
|
15
|
+
/** Whether to auto-start the MCP server. Defaults to false (manual start via env var). */
|
|
16
|
+
autoStart?: boolean;
|
|
17
|
+
/** Custom instructions for the MCP server. */
|
|
18
|
+
instructions?: string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* MCPServerPlugin — Kernel plugin that exposes ObjectStack as an MCP server.
|
|
22
|
+
*
|
|
23
|
+
* Lifecycle:
|
|
24
|
+
* 1. **init** — Creates {@link MCPServerRuntime} and registers as `'mcp'` service.
|
|
25
|
+
* 2. **start** — Bridges ToolRegistry, MetadataService, DataEngine, and Agents
|
|
26
|
+
* to the MCP server. Starts the transport if `autoStart` is enabled or
|
|
27
|
+
* the `MCP_SERVER_ENABLED` environment variable is set.
|
|
28
|
+
* 3. **destroy** — Stops the MCP transport.
|
|
29
|
+
*
|
|
30
|
+
* Environment Variables:
|
|
31
|
+
* - `MCP_SERVER_ENABLED=true` — Enable MCP server at startup
|
|
32
|
+
* - `MCP_SERVER_NAME` — Override server name
|
|
33
|
+
* - `MCP_SERVER_TRANSPORT` — Override transport ('stdio' | 'http')
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* import { LiteKernel } from '@objectstack/core';
|
|
38
|
+
* import { MCPServerPlugin } from '@objectstack/plugin-mcp-server';
|
|
39
|
+
*
|
|
40
|
+
* const kernel = new LiteKernel();
|
|
41
|
+
* kernel.use(new MCPServerPlugin({ autoStart: true }));
|
|
42
|
+
* await kernel.bootstrap();
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
declare class MCPServerPlugin implements Plugin {
|
|
46
|
+
name: string;
|
|
47
|
+
version: string;
|
|
48
|
+
type: "standard";
|
|
49
|
+
dependencies: string[];
|
|
50
|
+
private runtime?;
|
|
51
|
+
private readonly options;
|
|
52
|
+
constructor(options?: MCPServerPluginOptions);
|
|
53
|
+
init(ctx: PluginContext): Promise<void>;
|
|
54
|
+
start(ctx: PluginContext): Promise<void>;
|
|
55
|
+
destroy(): Promise<void>;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Minimal ToolRegistry interface consumed by the MCP bridge.
|
|
60
|
+
*
|
|
61
|
+
* Matches the public API of `ToolRegistry` from `@objectstack/service-ai`
|
|
62
|
+
* without creating a hard dependency on that package.
|
|
63
|
+
*/
|
|
64
|
+
interface ToolRegistry {
|
|
65
|
+
/** Return all registered tool definitions. */
|
|
66
|
+
getAll(): AIToolDefinition[];
|
|
67
|
+
/** Execute a tool call and return the result. */
|
|
68
|
+
execute(toolCall: ToolCallPart): Promise<ToolExecutionResult>;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Extended ToolResultPart with isError flag, matching service-ai's ToolExecutionResult.
|
|
72
|
+
*/
|
|
73
|
+
interface ToolExecutionResult extends ToolResultPart {
|
|
74
|
+
isError?: boolean;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Configuration for the MCP Server Runtime.
|
|
79
|
+
*/
|
|
80
|
+
interface MCPServerRuntimeConfig {
|
|
81
|
+
/** Human-readable server name. */
|
|
82
|
+
name?: string;
|
|
83
|
+
/** Server version (semver). */
|
|
84
|
+
version?: string;
|
|
85
|
+
/** Optional instructions describing how to use the server. */
|
|
86
|
+
instructions?: string;
|
|
87
|
+
/** Transport mode: 'stdio' (default) or 'http'. */
|
|
88
|
+
transport?: 'stdio' | 'http';
|
|
89
|
+
/** Logger instance. */
|
|
90
|
+
logger?: Logger;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* MCPServerRuntime — Bridges ObjectStack kernel services to the Model Context Protocol.
|
|
94
|
+
*
|
|
95
|
+
* Responsibilities:
|
|
96
|
+
* 1. Bridge ToolRegistry → MCP tools (all registered AI tools)
|
|
97
|
+
* 2. Bridge IMetadataService → MCP resources (object schemas, metadata types)
|
|
98
|
+
* 3. Bridge IDataEngine → MCP resources (record access by URI)
|
|
99
|
+
* 4. Bridge Agent definitions → MCP prompts (agent instructions)
|
|
100
|
+
*
|
|
101
|
+
* Architecture:
|
|
102
|
+
* ```
|
|
103
|
+
* ToolRegistry (service-ai) ──┐
|
|
104
|
+
* IMetadataService (metadata) ─┼──→ MCPServerRuntime ──→ McpServer (SDK)
|
|
105
|
+
* IDataEngine (objectql) ──┤ │
|
|
106
|
+
* Agent definitions ──┘ ├── stdio transport
|
|
107
|
+
* └── http transport (future)
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
declare class MCPServerRuntime {
|
|
111
|
+
private readonly mcpServer;
|
|
112
|
+
private readonly config;
|
|
113
|
+
private transport;
|
|
114
|
+
private started;
|
|
115
|
+
constructor(config?: MCPServerRuntimeConfig);
|
|
116
|
+
/** The underlying McpServer instance (for advanced use cases). */
|
|
117
|
+
get server(): McpServer;
|
|
118
|
+
/** Whether the server is currently connected and running. */
|
|
119
|
+
get isStarted(): boolean;
|
|
120
|
+
/**
|
|
121
|
+
* Extract the text value from a ToolExecutionResult's output.
|
|
122
|
+
*
|
|
123
|
+
* The output may be a `{ type: 'text', value: string }` object (from the
|
|
124
|
+
* Vercel AI SDK ToolResultPart) or any serialisable value.
|
|
125
|
+
*/
|
|
126
|
+
private static formatToolOutput;
|
|
127
|
+
/**
|
|
128
|
+
* Bridge all tools from the ToolRegistry to MCP tools.
|
|
129
|
+
*
|
|
130
|
+
* Each registered tool becomes an MCP tool with the same name, description,
|
|
131
|
+
* and JSON Schema parameters. The handler delegates to the ToolRegistry's
|
|
132
|
+
* execute path.
|
|
133
|
+
*/
|
|
134
|
+
bridgeTools(toolRegistry: ToolRegistry): void;
|
|
135
|
+
/**
|
|
136
|
+
* Register a single tool on the MCP server from an AIToolDefinition.
|
|
137
|
+
*/
|
|
138
|
+
private registerToolFromDefinition;
|
|
139
|
+
/**
|
|
140
|
+
* Check if a tool is read-only (data query tools).
|
|
141
|
+
*/
|
|
142
|
+
private isReadOnlyTool;
|
|
143
|
+
/**
|
|
144
|
+
* Check if a tool performs destructive operations.
|
|
145
|
+
*/
|
|
146
|
+
private isDestructiveTool;
|
|
147
|
+
/**
|
|
148
|
+
* Bridge metadata service and data engine to MCP resources.
|
|
149
|
+
*
|
|
150
|
+
* Exposes:
|
|
151
|
+
* - `objectstack://objects` — List all data objects
|
|
152
|
+
* - `objectstack://objects/{objectName}` — Get object schema
|
|
153
|
+
* - `objectstack://objects/{objectName}/records/{recordId}` — Get a specific record
|
|
154
|
+
* - `objectstack://metadata/types` — List all metadata types
|
|
155
|
+
*/
|
|
156
|
+
bridgeResources(metadataService: IMetadataService, dataEngine?: IDataEngine): void;
|
|
157
|
+
/**
|
|
158
|
+
* Bridge registered agents to MCP prompts.
|
|
159
|
+
*
|
|
160
|
+
* Each active agent becomes an MCP prompt with:
|
|
161
|
+
* - Name matching the agent name
|
|
162
|
+
* - System message from agent instructions
|
|
163
|
+
* - Optional context arguments (objectName, recordId, viewName)
|
|
164
|
+
*/
|
|
165
|
+
bridgePrompts(metadataService: IMetadataService): void;
|
|
166
|
+
/**
|
|
167
|
+
* Start the MCP server with the configured transport.
|
|
168
|
+
*
|
|
169
|
+
* For stdio transport, this connects to process stdin/stdout.
|
|
170
|
+
*/
|
|
171
|
+
start(): Promise<void>;
|
|
172
|
+
/**
|
|
173
|
+
* Stop the MCP server and disconnect the transport.
|
|
174
|
+
*/
|
|
175
|
+
stop(): Promise<void>;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export { MCPServerPlugin, type MCPServerPluginOptions, MCPServerRuntime, type MCPServerRuntimeConfig };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import { Plugin, PluginContext } from '@objectstack/core';
|
|
2
|
+
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
|
|
3
|
+
import { AIToolDefinition, ToolCallPart, ToolResultPart, Logger, IMetadataService, IDataEngine } from '@objectstack/spec/contracts';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Configuration options for the MCPServerPlugin.
|
|
7
|
+
*/
|
|
8
|
+
interface MCPServerPluginOptions {
|
|
9
|
+
/** Override MCP server name. Defaults to 'objectstack'. */
|
|
10
|
+
name?: string;
|
|
11
|
+
/** Override MCP server version. Defaults to package version. */
|
|
12
|
+
version?: string;
|
|
13
|
+
/** Transport mode: 'stdio' (default). */
|
|
14
|
+
transport?: 'stdio' | 'http';
|
|
15
|
+
/** Whether to auto-start the MCP server. Defaults to false (manual start via env var). */
|
|
16
|
+
autoStart?: boolean;
|
|
17
|
+
/** Custom instructions for the MCP server. */
|
|
18
|
+
instructions?: string;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* MCPServerPlugin — Kernel plugin that exposes ObjectStack as an MCP server.
|
|
22
|
+
*
|
|
23
|
+
* Lifecycle:
|
|
24
|
+
* 1. **init** — Creates {@link MCPServerRuntime} and registers as `'mcp'` service.
|
|
25
|
+
* 2. **start** — Bridges ToolRegistry, MetadataService, DataEngine, and Agents
|
|
26
|
+
* to the MCP server. Starts the transport if `autoStart` is enabled or
|
|
27
|
+
* the `MCP_SERVER_ENABLED` environment variable is set.
|
|
28
|
+
* 3. **destroy** — Stops the MCP transport.
|
|
29
|
+
*
|
|
30
|
+
* Environment Variables:
|
|
31
|
+
* - `MCP_SERVER_ENABLED=true` — Enable MCP server at startup
|
|
32
|
+
* - `MCP_SERVER_NAME` — Override server name
|
|
33
|
+
* - `MCP_SERVER_TRANSPORT` — Override transport ('stdio' | 'http')
|
|
34
|
+
*
|
|
35
|
+
* @example
|
|
36
|
+
* ```ts
|
|
37
|
+
* import { LiteKernel } from '@objectstack/core';
|
|
38
|
+
* import { MCPServerPlugin } from '@objectstack/plugin-mcp-server';
|
|
39
|
+
*
|
|
40
|
+
* const kernel = new LiteKernel();
|
|
41
|
+
* kernel.use(new MCPServerPlugin({ autoStart: true }));
|
|
42
|
+
* await kernel.bootstrap();
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
declare class MCPServerPlugin implements Plugin {
|
|
46
|
+
name: string;
|
|
47
|
+
version: string;
|
|
48
|
+
type: "standard";
|
|
49
|
+
dependencies: string[];
|
|
50
|
+
private runtime?;
|
|
51
|
+
private readonly options;
|
|
52
|
+
constructor(options?: MCPServerPluginOptions);
|
|
53
|
+
init(ctx: PluginContext): Promise<void>;
|
|
54
|
+
start(ctx: PluginContext): Promise<void>;
|
|
55
|
+
destroy(): Promise<void>;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Minimal ToolRegistry interface consumed by the MCP bridge.
|
|
60
|
+
*
|
|
61
|
+
* Matches the public API of `ToolRegistry` from `@objectstack/service-ai`
|
|
62
|
+
* without creating a hard dependency on that package.
|
|
63
|
+
*/
|
|
64
|
+
interface ToolRegistry {
|
|
65
|
+
/** Return all registered tool definitions. */
|
|
66
|
+
getAll(): AIToolDefinition[];
|
|
67
|
+
/** Execute a tool call and return the result. */
|
|
68
|
+
execute(toolCall: ToolCallPart): Promise<ToolExecutionResult>;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Extended ToolResultPart with isError flag, matching service-ai's ToolExecutionResult.
|
|
72
|
+
*/
|
|
73
|
+
interface ToolExecutionResult extends ToolResultPart {
|
|
74
|
+
isError?: boolean;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Configuration for the MCP Server Runtime.
|
|
79
|
+
*/
|
|
80
|
+
interface MCPServerRuntimeConfig {
|
|
81
|
+
/** Human-readable server name. */
|
|
82
|
+
name?: string;
|
|
83
|
+
/** Server version (semver). */
|
|
84
|
+
version?: string;
|
|
85
|
+
/** Optional instructions describing how to use the server. */
|
|
86
|
+
instructions?: string;
|
|
87
|
+
/** Transport mode: 'stdio' (default) or 'http'. */
|
|
88
|
+
transport?: 'stdio' | 'http';
|
|
89
|
+
/** Logger instance. */
|
|
90
|
+
logger?: Logger;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* MCPServerRuntime — Bridges ObjectStack kernel services to the Model Context Protocol.
|
|
94
|
+
*
|
|
95
|
+
* Responsibilities:
|
|
96
|
+
* 1. Bridge ToolRegistry → MCP tools (all registered AI tools)
|
|
97
|
+
* 2. Bridge IMetadataService → MCP resources (object schemas, metadata types)
|
|
98
|
+
* 3. Bridge IDataEngine → MCP resources (record access by URI)
|
|
99
|
+
* 4. Bridge Agent definitions → MCP prompts (agent instructions)
|
|
100
|
+
*
|
|
101
|
+
* Architecture:
|
|
102
|
+
* ```
|
|
103
|
+
* ToolRegistry (service-ai) ──┐
|
|
104
|
+
* IMetadataService (metadata) ─┼──→ MCPServerRuntime ──→ McpServer (SDK)
|
|
105
|
+
* IDataEngine (objectql) ──┤ │
|
|
106
|
+
* Agent definitions ──┘ ├── stdio transport
|
|
107
|
+
* └── http transport (future)
|
|
108
|
+
* ```
|
|
109
|
+
*/
|
|
110
|
+
declare class MCPServerRuntime {
|
|
111
|
+
private readonly mcpServer;
|
|
112
|
+
private readonly config;
|
|
113
|
+
private transport;
|
|
114
|
+
private started;
|
|
115
|
+
constructor(config?: MCPServerRuntimeConfig);
|
|
116
|
+
/** The underlying McpServer instance (for advanced use cases). */
|
|
117
|
+
get server(): McpServer;
|
|
118
|
+
/** Whether the server is currently connected and running. */
|
|
119
|
+
get isStarted(): boolean;
|
|
120
|
+
/**
|
|
121
|
+
* Extract the text value from a ToolExecutionResult's output.
|
|
122
|
+
*
|
|
123
|
+
* The output may be a `{ type: 'text', value: string }` object (from the
|
|
124
|
+
* Vercel AI SDK ToolResultPart) or any serialisable value.
|
|
125
|
+
*/
|
|
126
|
+
private static formatToolOutput;
|
|
127
|
+
/**
|
|
128
|
+
* Bridge all tools from the ToolRegistry to MCP tools.
|
|
129
|
+
*
|
|
130
|
+
* Each registered tool becomes an MCP tool with the same name, description,
|
|
131
|
+
* and JSON Schema parameters. The handler delegates to the ToolRegistry's
|
|
132
|
+
* execute path.
|
|
133
|
+
*/
|
|
134
|
+
bridgeTools(toolRegistry: ToolRegistry): void;
|
|
135
|
+
/**
|
|
136
|
+
* Register a single tool on the MCP server from an AIToolDefinition.
|
|
137
|
+
*/
|
|
138
|
+
private registerToolFromDefinition;
|
|
139
|
+
/**
|
|
140
|
+
* Check if a tool is read-only (data query tools).
|
|
141
|
+
*/
|
|
142
|
+
private isReadOnlyTool;
|
|
143
|
+
/**
|
|
144
|
+
* Check if a tool performs destructive operations.
|
|
145
|
+
*/
|
|
146
|
+
private isDestructiveTool;
|
|
147
|
+
/**
|
|
148
|
+
* Bridge metadata service and data engine to MCP resources.
|
|
149
|
+
*
|
|
150
|
+
* Exposes:
|
|
151
|
+
* - `objectstack://objects` — List all data objects
|
|
152
|
+
* - `objectstack://objects/{objectName}` — Get object schema
|
|
153
|
+
* - `objectstack://objects/{objectName}/records/{recordId}` — Get a specific record
|
|
154
|
+
* - `objectstack://metadata/types` — List all metadata types
|
|
155
|
+
*/
|
|
156
|
+
bridgeResources(metadataService: IMetadataService, dataEngine?: IDataEngine): void;
|
|
157
|
+
/**
|
|
158
|
+
* Bridge registered agents to MCP prompts.
|
|
159
|
+
*
|
|
160
|
+
* Each active agent becomes an MCP prompt with:
|
|
161
|
+
* - Name matching the agent name
|
|
162
|
+
* - System message from agent instructions
|
|
163
|
+
* - Optional context arguments (objectName, recordId, viewName)
|
|
164
|
+
*/
|
|
165
|
+
bridgePrompts(metadataService: IMetadataService): void;
|
|
166
|
+
/**
|
|
167
|
+
* Start the MCP server with the configured transport.
|
|
168
|
+
*
|
|
169
|
+
* For stdio transport, this connects to process stdin/stdout.
|
|
170
|
+
*/
|
|
171
|
+
start(): Promise<void>;
|
|
172
|
+
/**
|
|
173
|
+
* Stop the MCP server and disconnect the transport.
|
|
174
|
+
*/
|
|
175
|
+
stop(): Promise<void>;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
export { MCPServerPlugin, type MCPServerPluginOptions, MCPServerRuntime, type MCPServerRuntimeConfig };
|