prism-mcp-server 20.0.7 → 20.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.
@@ -1,234 +0,0 @@
1
- /**
2
- * MCP Bridge — Connect external MCP servers to the Prism Agent Terminal
3
- * ======================================================================
4
- *
5
- * Allows `prism prompt` to discover and connect to MCP servers configured
6
- * in standard config files (.cursor/mcp.json, .vscode/mcp.json) or via
7
- * manual `prism prompt --mcp <command>`.
8
- *
9
- * Connected MCP server tools are automatically injected into Gemini's
10
- * function declarations so the AI can call them during conversation.
11
- *
12
- * Config format (standard MCP convention):
13
- * ```json
14
- * {
15
- * "mcpServers": {
16
- * "my-server": {
17
- * "command": "node",
18
- * "args": ["path/to/server.js"],
19
- * "env": { "API_KEY": "..." }
20
- * }
21
- * }
22
- * }
23
- * ```
24
- */
25
- import * as fs from "fs";
26
- import * as path from "path";
27
- import { Client } from "@modelcontextprotocol/sdk/client/index.js";
28
- import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
29
- import { SchemaType } from "@google/generative-ai";
30
- // ---------------------------------------------------------------------------
31
- // MCP Bridge
32
- // ---------------------------------------------------------------------------
33
- export class McpBridge {
34
- connections = new Map();
35
- /**
36
- * Connect to an MCP server via stdio.
37
- */
38
- async connect(serverName, config) {
39
- // Don't double-connect
40
- if (this.connections.has(serverName)) {
41
- const existing = this.connections.get(serverName);
42
- return existing.tools;
43
- }
44
- const client = new Client({ name: "prism-agent", version: "12.0.0" }, { capabilities: {} });
45
- const transport = new StdioClientTransport({
46
- command: config.command,
47
- args: config.args || [],
48
- env: { ...process.env, ...(config.env || {}) },
49
- });
50
- await client.connect(transport);
51
- // Discover tools
52
- const toolsResult = await client.listTools();
53
- const tools = toolsResult.tools.map((t) => ({
54
- name: t.name,
55
- description: t.description || "",
56
- inputSchema: t.inputSchema,
57
- }));
58
- this.connections.set(serverName, { name: serverName, client, transport, tools });
59
- return tools;
60
- }
61
- /**
62
- * Call a tool on a connected MCP server.
63
- */
64
- async callTool(toolName, args) {
65
- // Find which server owns this tool
66
- for (const conn of this.connections.values()) {
67
- const tool = conn.tools.find((t) => t.name === toolName);
68
- if (!tool)
69
- continue;
70
- const result = await conn.client.callTool({
71
- name: toolName,
72
- arguments: args,
73
- });
74
- if (result.isError) {
75
- const errText = result.content
76
- .filter((c) => c.type === "text")
77
- .map((c) => c.text)
78
- .join("\n");
79
- return `Error: ${errText || "Tool returned an error"}`;
80
- }
81
- // Extract text content
82
- const texts = result.content
83
- .filter((c) => c.type === "text")
84
- .map((c) => c.text || "");
85
- return texts.join("\n") || "(no output)";
86
- }
87
- return `Error: No MCP server has tool "${toolName}"`;
88
- }
89
- /**
90
- * Get all tools from all connected servers as Gemini FunctionDeclarations.
91
- * Prefixes tool names with server name to avoid collisions.
92
- */
93
- getGeminiFunctionDeclarations() {
94
- const declarations = [];
95
- for (const conn of this.connections.values()) {
96
- for (const tool of conn.tools) {
97
- // Convert JSON Schema to Gemini's FunctionDeclaration format
98
- declarations.push({
99
- name: tool.name,
100
- description: `[MCP:${conn.name}] ${tool.description}`,
101
- parameters: convertJsonSchemaToGemini(tool.inputSchema),
102
- });
103
- }
104
- }
105
- return declarations;
106
- }
107
- /**
108
- * Check if a tool name belongs to an MCP server.
109
- */
110
- hasToolName(toolName) {
111
- for (const conn of this.connections.values()) {
112
- if (conn.tools.some((t) => t.name === toolName))
113
- return true;
114
- }
115
- return false;
116
- }
117
- /**
118
- * List all connected servers and their tools.
119
- */
120
- listServers() {
121
- return Array.from(this.connections.values()).map((c) => ({
122
- name: c.name,
123
- toolCount: c.tools.length,
124
- tools: c.tools.map((t) => t.name),
125
- }));
126
- }
127
- /**
128
- * Disconnect all servers gracefully.
129
- */
130
- async disconnectAll() {
131
- for (const conn of this.connections.values()) {
132
- try {
133
- await conn.client.close();
134
- }
135
- catch {
136
- // Ignore cleanup errors
137
- }
138
- }
139
- this.connections.clear();
140
- }
141
- }
142
- // ---------------------------------------------------------------------------
143
- // Config Discovery
144
- // ---------------------------------------------------------------------------
145
- /**
146
- * Search for MCP server configurations in standard locations.
147
- * Returns merged config from all discovered files.
148
- */
149
- export function discoverMcpConfigs(cwd = process.cwd()) {
150
- const configs = {};
151
- // Search paths in order of priority (later overrides earlier)
152
- const searchPaths = [
153
- // Global configs
154
- path.join(process.env.HOME || "~", ".cursor", "mcp.json"),
155
- path.join(process.env.HOME || "~", ".vscode", "mcp.json"),
156
- // Project-level configs (higher priority)
157
- path.join(cwd, ".cursor", "mcp.json"),
158
- path.join(cwd, ".vscode", "mcp.json"),
159
- path.join(cwd, "mcp.json"),
160
- ];
161
- for (const configPath of searchPaths) {
162
- try {
163
- if (!fs.existsSync(configPath))
164
- continue;
165
- const raw = fs.readFileSync(configPath, "utf-8");
166
- const parsed = JSON.parse(raw);
167
- // Support both { mcpServers: {...} } and { servers: {...} } formats
168
- const servers = parsed.mcpServers || parsed.servers || {};
169
- for (const [name, config] of Object.entries(servers)) {
170
- const c = config;
171
- if (c.command) {
172
- configs[name] = c;
173
- }
174
- }
175
- }
176
- catch {
177
- // Skip invalid config files
178
- }
179
- }
180
- return configs;
181
- }
182
- // ---------------------------------------------------------------------------
183
- // Schema Conversion
184
- // ---------------------------------------------------------------------------
185
- /**
186
- * Convert a JSON Schema object to Gemini's FunctionDeclaration parameters format.
187
- * Gemini uses its own SchemaType enum instead of standard JSON Schema type strings.
188
- */
189
- function convertJsonSchemaToGemini(schema) {
190
- if (!schema || typeof schema !== "object") {
191
- return { type: SchemaType.OBJECT, properties: {} };
192
- }
193
- const properties = {};
194
- const schemaProps = (schema.properties || {});
195
- for (const [key, prop] of Object.entries(schemaProps)) {
196
- properties[key] = convertPropertyToGemini(prop);
197
- }
198
- // eslint-disable-next-line @typescript-eslint/no-explicit-any -- dynamic JSON Schema conversion
199
- return {
200
- type: SchemaType.OBJECT,
201
- properties,
202
- required: schema.required || [],
203
- };
204
- }
205
- function convertPropertyToGemini(prop) {
206
- const typeMap = {
207
- string: SchemaType.STRING,
208
- number: SchemaType.NUMBER,
209
- integer: SchemaType.INTEGER,
210
- boolean: SchemaType.BOOLEAN,
211
- array: SchemaType.ARRAY,
212
- object: SchemaType.OBJECT,
213
- };
214
- const result = {
215
- type: typeMap[prop.type] || SchemaType.STRING,
216
- };
217
- if (prop.description)
218
- result.description = prop.description;
219
- if (prop.enum)
220
- result.enum = prop.enum;
221
- // Handle array items
222
- if (prop.type === "array" && prop.items) {
223
- result.items = convertPropertyToGemini(prop.items);
224
- }
225
- // Handle nested objects
226
- if (prop.type === "object" && prop.properties) {
227
- const nestedProps = {};
228
- for (const [k, v] of Object.entries(prop.properties)) {
229
- nestedProps[k] = convertPropertyToGemini(v);
230
- }
231
- result.properties = nestedProps;
232
- }
233
- return result;
234
- }