@portel/mcp 1.0.0 → 1.0.1
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/README.md +227 -0
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
# @portel/mcp
|
|
2
|
+
|
|
3
|
+
MCP (Model Context Protocol) client library for connecting to and interacting with MCP servers. Provides transport abstraction, configuration management, and user elicitation utilities.
|
|
4
|
+
|
|
5
|
+
## Part of the Portel Ecosystem
|
|
6
|
+
|
|
7
|
+
```
|
|
8
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
9
|
+
│ @portel/cli │
|
|
10
|
+
│ CLI utilities: formatting, progress, logging │
|
|
11
|
+
└─────────────────────────────────────────────────────────────────┘
|
|
12
|
+
▲
|
|
13
|
+
│
|
|
14
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
15
|
+
│ @portel/mcp (this package) │
|
|
16
|
+
│ MCP protocol: client, transport, config │
|
|
17
|
+
└─────────────────────────────────────────────────────────────────┘
|
|
18
|
+
▲
|
|
19
|
+
│
|
|
20
|
+
┌─────────────────────────────────────────────────────────────────┐
|
|
21
|
+
│ @portel/photon-core │
|
|
22
|
+
│ Core library: schema extraction, generators, UI │
|
|
23
|
+
└─────────────────────────────────────────────────────────────────┘
|
|
24
|
+
▲
|
|
25
|
+
│
|
|
26
|
+
┌─────────────────────┼─────────────────────┐
|
|
27
|
+
│ │ │
|
|
28
|
+
┌───────┴───────┐ ┌───────┴───────┐ ┌───────┴───────┐
|
|
29
|
+
│ @portel/photon│ │ lumina │ │ @portel/ncp │
|
|
30
|
+
│ CLI + BEAM │ │ REST runtime │ │ MCP orchestr. │
|
|
31
|
+
└───────────────┘ └───────────────┘ └───────────────┘
|
|
32
|
+
```
|
|
33
|
+
|
|
34
|
+
**Use this package if:** You're building tools that need to connect to MCP servers, call MCP tools, or manage MCP server configurations.
|
|
35
|
+
|
|
36
|
+
## Installation
|
|
37
|
+
|
|
38
|
+
```bash
|
|
39
|
+
npm install @portel/mcp
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Features
|
|
43
|
+
|
|
44
|
+
### MCP Client
|
|
45
|
+
|
|
46
|
+
Connect to MCP servers and call tools.
|
|
47
|
+
|
|
48
|
+
```typescript
|
|
49
|
+
import { MCPClient, SDKMCPClientFactory } from '@portel/mcp';
|
|
50
|
+
|
|
51
|
+
// Create client factory
|
|
52
|
+
const factory = new SDKMCPClientFactory();
|
|
53
|
+
|
|
54
|
+
// Connect to an MCP server
|
|
55
|
+
const client = await factory.create({
|
|
56
|
+
command: 'npx',
|
|
57
|
+
args: ['-y', '@anthropic/weather-mcp'],
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
// List available tools
|
|
61
|
+
const tools = await client.listTools();
|
|
62
|
+
console.log(tools);
|
|
63
|
+
|
|
64
|
+
// Call a tool
|
|
65
|
+
const result = await client.callTool('get-weather', { city: 'London' });
|
|
66
|
+
console.log(result);
|
|
67
|
+
|
|
68
|
+
// Disconnect when done
|
|
69
|
+
await client.disconnect();
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### MCP Proxy
|
|
73
|
+
|
|
74
|
+
Create a proxy object that maps tool calls to method calls.
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
import { createMCPProxy, SDKMCPClientFactory } from '@portel/mcp';
|
|
78
|
+
|
|
79
|
+
const factory = new SDKMCPClientFactory();
|
|
80
|
+
const client = await factory.create(config);
|
|
81
|
+
|
|
82
|
+
// Create proxy - tool names become methods
|
|
83
|
+
const mcp = createMCPProxy(client);
|
|
84
|
+
|
|
85
|
+
// Call tools as methods
|
|
86
|
+
const weather = await mcp.getWeather({ city: 'Tokyo' });
|
|
87
|
+
const forecast = await mcp.getForecast({ days: 5 });
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### Configuration Management
|
|
91
|
+
|
|
92
|
+
Load and save MCP server configurations (Claude Desktop format).
|
|
93
|
+
|
|
94
|
+
```typescript
|
|
95
|
+
import {
|
|
96
|
+
loadPhotonMCPConfig,
|
|
97
|
+
savePhotonMCPConfig,
|
|
98
|
+
getMCPServerConfig,
|
|
99
|
+
setMCPServerConfig,
|
|
100
|
+
listMCPServers,
|
|
101
|
+
} from '@portel/mcp';
|
|
102
|
+
|
|
103
|
+
// Load config from ~/.photon/mcp-servers.json
|
|
104
|
+
const config = loadPhotonMCPConfig();
|
|
105
|
+
|
|
106
|
+
// List configured servers
|
|
107
|
+
const servers = listMCPServers();
|
|
108
|
+
// => ['weather', 'database', 'github']
|
|
109
|
+
|
|
110
|
+
// Get specific server config
|
|
111
|
+
const weatherConfig = getMCPServerConfig('weather');
|
|
112
|
+
|
|
113
|
+
// Add/update a server
|
|
114
|
+
setMCPServerConfig('my-server', {
|
|
115
|
+
command: 'node',
|
|
116
|
+
args: ['./my-server.js'],
|
|
117
|
+
env: { API_KEY: '${MY_API_KEY}' },
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
// Save changes
|
|
121
|
+
savePhotonMCPConfig(config);
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
### User Elicitation
|
|
125
|
+
|
|
126
|
+
Prompt users for input during tool execution.
|
|
127
|
+
|
|
128
|
+
```typescript
|
|
129
|
+
import { prompt, confirm, elicit, setElicitHandler } from '@portel/mcp';
|
|
130
|
+
|
|
131
|
+
// Simple prompts
|
|
132
|
+
const name = await prompt('What is your name?');
|
|
133
|
+
const proceed = await confirm('Continue?');
|
|
134
|
+
|
|
135
|
+
// Rich elicitation with schema
|
|
136
|
+
const result = await elicit({
|
|
137
|
+
message: 'Configure settings',
|
|
138
|
+
schema: {
|
|
139
|
+
type: 'object',
|
|
140
|
+
properties: {
|
|
141
|
+
theme: { type: 'string', enum: ['light', 'dark'] },
|
|
142
|
+
fontSize: { type: 'number', minimum: 8, maximum: 24 },
|
|
143
|
+
},
|
|
144
|
+
},
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
// Custom handler (for different UIs)
|
|
148
|
+
setElicitHandler(async (options) => {
|
|
149
|
+
// Custom UI implementation
|
|
150
|
+
return { action: 'submit', data: formData };
|
|
151
|
+
});
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
## API Reference
|
|
155
|
+
|
|
156
|
+
### MCP Client
|
|
157
|
+
|
|
158
|
+
| Export | Description |
|
|
159
|
+
|--------|-------------|
|
|
160
|
+
| `MCPClient` | Core MCP client class |
|
|
161
|
+
| `MCPClientFactory` | Factory interface for creating clients |
|
|
162
|
+
| `SDKMCPClientFactory` | Factory using @modelcontextprotocol/sdk |
|
|
163
|
+
| `createMCPProxy(client)` | Create proxy for tool-as-method calls |
|
|
164
|
+
| `MCPError` | Base error class |
|
|
165
|
+
| `MCPNotConnectedError` | Client not connected error |
|
|
166
|
+
| `MCPToolError` | Tool execution error |
|
|
167
|
+
|
|
168
|
+
### Configuration
|
|
169
|
+
|
|
170
|
+
| Export | Description |
|
|
171
|
+
|--------|-------------|
|
|
172
|
+
| `loadPhotonMCPConfig()` | Load config from ~/.photon |
|
|
173
|
+
| `savePhotonMCPConfig(config)` | Save config to ~/.photon |
|
|
174
|
+
| `getMCPServerConfig(name)` | Get server config by name |
|
|
175
|
+
| `setMCPServerConfig(name, config)` | Add/update server config |
|
|
176
|
+
| `removeMCPServerConfig(name)` | Remove server config |
|
|
177
|
+
| `listMCPServers()` | List all configured servers |
|
|
178
|
+
| `isMCPConfigured(name)` | Check if server is configured |
|
|
179
|
+
| `resolveEnvVars(config)` | Resolve ${VAR} in config |
|
|
180
|
+
|
|
181
|
+
### Elicitation
|
|
182
|
+
|
|
183
|
+
| Export | Description |
|
|
184
|
+
|--------|-------------|
|
|
185
|
+
| `prompt(message)` | Simple text prompt |
|
|
186
|
+
| `confirm(message)` | Yes/no confirmation |
|
|
187
|
+
| `elicit(options)` | Rich form elicitation |
|
|
188
|
+
| `setPromptHandler(handler)` | Set custom prompt handler |
|
|
189
|
+
| `setElicitHandler(handler)` | Set custom elicit handler |
|
|
190
|
+
| `elicitReadline` | Readline-based handler |
|
|
191
|
+
| `elicitNativeDialog` | Native dialog handler |
|
|
192
|
+
|
|
193
|
+
### Types
|
|
194
|
+
|
|
195
|
+
```typescript
|
|
196
|
+
interface MCPServerConfig {
|
|
197
|
+
command: string;
|
|
198
|
+
args?: string[];
|
|
199
|
+
env?: Record<string, string>;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
interface MCPToolInfo {
|
|
203
|
+
name: string;
|
|
204
|
+
description?: string;
|
|
205
|
+
inputSchema?: object;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
interface MCPToolResult {
|
|
209
|
+
content: Array<{ type: string; text?: string }>;
|
|
210
|
+
isError?: boolean;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
interface ElicitOptions {
|
|
214
|
+
message: string;
|
|
215
|
+
schema?: object;
|
|
216
|
+
requestedFields?: string[];
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
interface ElicitResult {
|
|
220
|
+
action: 'submit' | 'cancel' | 'skip';
|
|
221
|
+
data?: Record<string, any>;
|
|
222
|
+
}
|
|
223
|
+
```
|
|
224
|
+
|
|
225
|
+
## License
|
|
226
|
+
|
|
227
|
+
MIT
|