@myatkyawthu/mcp-connect 0.1.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/LICENSE +21 -0
- package/README.md +308 -0
- package/package.json +63 -0
- package/src/cli.js +170 -0
- package/src/defineMCP.js +72 -0
- package/src/index.js +35 -0
- package/src/server/mcpServer.js +236 -0
- package/src/types/mcp.js +108 -0
- package/src/utils/configValidation.js +381 -0
- package/src/utils/logger.js +290 -0
- package/src/utils/validation.js +78 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 myat-kyaw-thu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
# mcp-connect
|
|
2
|
+
|
|
3
|
+
> Dead simple MCP (Model Context Protocol) server for exposing your app functions to AI agents
|
|
4
|
+
|
|
5
|
+
[](https://www.npmjs.com/package/mcp-connect)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
|
|
8
|
+
## 🚀 Quick Start
|
|
9
|
+
|
|
10
|
+
### Option 1: Global Installation (Recommended)
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
# Install globally with npm
|
|
14
|
+
npm install -g mcp-connect
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Create `mcp.config.js` in your project:
|
|
18
|
+
|
|
19
|
+
```javascript
|
|
20
|
+
import { defineMCP } from "mcp-connect"
|
|
21
|
+
|
|
22
|
+
export default defineMCP({
|
|
23
|
+
name: "My App",
|
|
24
|
+
version: "1.0.0",
|
|
25
|
+
tools: [
|
|
26
|
+
["hello", async ({ name }) => `Hello ${name}!`],
|
|
27
|
+
["getTodos", async () => [{ id: 1, title: "Buy milk" }]],
|
|
28
|
+
]
|
|
29
|
+
})
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Start the server from your project directory:
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
mcp-connect
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
### Option 2: Local Installation
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
# Install locally with npm
|
|
42
|
+
npm install mcp-connect
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Start with npx:
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
npx mcp-connect
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Connect your AI agent via STDIO transport (Claude Desktop)!
|
|
52
|
+
|
|
53
|
+
## 🎯 Features
|
|
54
|
+
|
|
55
|
+
- **Zero Config** - Works out of the box with minimal setup
|
|
56
|
+
- **MCP Compliant** - Uses official MCP SDK with JSON-RPC 2.0
|
|
57
|
+
- **STDIO Transport** - Direct communication with Claude Desktop and other MCP clients
|
|
58
|
+
- **Pure JavaScript** - No compilation needed, runs directly on Node.js 18+
|
|
59
|
+
- **Production Ready** - Enterprise-grade error handling and logging
|
|
60
|
+
- **Performance Monitoring** - Built-in execution timing and debugging
|
|
61
|
+
- **Universal** - Works with npm, pnpm, and yarn
|
|
62
|
+
- **Claude Desktop Ready** - Works seamlessly with Claude Desktop and other MCP clients
|
|
63
|
+
- **Simple API** - Clean, declarative tool definitions with flexible formats
|
|
64
|
+
|
|
65
|
+
## 📖 Documentation
|
|
66
|
+
|
|
67
|
+
### Tool Definition Formats
|
|
68
|
+
|
|
69
|
+
```javascript
|
|
70
|
+
// Tuple format (simple)
|
|
71
|
+
["toolName", async (args) => result]
|
|
72
|
+
|
|
73
|
+
// Object format (with metadata)
|
|
74
|
+
{
|
|
75
|
+
name: "toolName",
|
|
76
|
+
description: "What this tool does",
|
|
77
|
+
handler: async (args) => result,
|
|
78
|
+
schema: { /* JSON schema for input validation */ }
|
|
79
|
+
}
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
### Configuration Options
|
|
83
|
+
|
|
84
|
+
```javascript
|
|
85
|
+
defineMCP({
|
|
86
|
+
name: "My MCP Server", // Required: Server name
|
|
87
|
+
version: "1.0.0", // Required: Server version
|
|
88
|
+
description: "My server", // Optional: Description
|
|
89
|
+
tools: [
|
|
90
|
+
// Your tool definitions
|
|
91
|
+
]
|
|
92
|
+
})
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
## 🔧 CLI Usage
|
|
96
|
+
|
|
97
|
+
### Global Installation
|
|
98
|
+
|
|
99
|
+
```bash
|
|
100
|
+
# Start server (looks for mcp.config.ts in current directory)
|
|
101
|
+
mcp-connect
|
|
102
|
+
|
|
103
|
+
# With debug logging and performance tracking
|
|
104
|
+
MCP_DEBUG=1 mcp-connect
|
|
105
|
+
|
|
106
|
+
# Performance tracking only
|
|
107
|
+
MCP_PERF=1 mcp-connect
|
|
108
|
+
|
|
109
|
+
# Custom log level
|
|
110
|
+
MCP_LOG_LEVEL=warn mcp-connect
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### Local Installation
|
|
114
|
+
|
|
115
|
+
```bash
|
|
116
|
+
# Using npx
|
|
117
|
+
npx mcp-connect
|
|
118
|
+
|
|
119
|
+
# With environment variables
|
|
120
|
+
MCP_DEBUG=1 npx mcp-connect
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## 🔍 Logging & Debugging
|
|
124
|
+
|
|
125
|
+
MCP-Connect includes comprehensive logging and debugging features:
|
|
126
|
+
|
|
127
|
+
### Environment Variables
|
|
128
|
+
|
|
129
|
+
- `MCP_DEBUG=1` - Enable debug logging with full MCP message tracing
|
|
130
|
+
- `MCP_PERF=1` - Enable performance tracking for tool execution times
|
|
131
|
+
- `MCP_LOG_LEVEL=level` - Set minimum log level (debug, info, warn, error)
|
|
132
|
+
|
|
133
|
+
### Log Output Examples
|
|
134
|
+
|
|
135
|
+
```
|
|
136
|
+
[MCP-INFO] 2024-01-15T10:30:45.123Z MCP server "Todo App" started (stdio, 3 tools)
|
|
137
|
+
[MCP-DEBUG] 2024-01-15T10:30:46.456Z Tool execution started: addTodo [req:abc123]
|
|
138
|
+
[MCP-INFO] 2024-01-15T10:30:46.478Z Tool execution completed: addTodo (22ms) [req:abc123]
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
## ✅ Configuration Validation
|
|
142
|
+
|
|
143
|
+
MCP-Connect provides comprehensive configuration validation with helpful error messages:
|
|
144
|
+
|
|
145
|
+
```
|
|
146
|
+
❌ Configuration Errors:
|
|
147
|
+
1. tools[0].name: Tool name must be a non-empty string
|
|
148
|
+
Current value: ""
|
|
149
|
+
Suggestion: "myToolName"
|
|
150
|
+
|
|
151
|
+
⚠️ Configuration Warnings:
|
|
152
|
+
1. version: Version doesn't follow semantic versioning
|
|
153
|
+
Suggestion: Use format: "1.0.0"
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## 🛡️ Error Handling
|
|
157
|
+
|
|
158
|
+
- **Tool Execution Timeouts** - 30-second default timeout prevents hanging
|
|
159
|
+
- **Graceful Shutdown** - Proper cleanup on SIGINT/SIGTERM
|
|
160
|
+
- **Sanitized Errors** - Safe error messages without sensitive information
|
|
161
|
+
- **MCP-Compliant Errors** - Proper JSON-RPC error format
|
|
162
|
+
|
|
163
|
+
## 🤝 AI Agent Integration
|
|
164
|
+
|
|
165
|
+
### Claude Desktop
|
|
166
|
+
|
|
167
|
+
#### Global Installation (Recommended)
|
|
168
|
+
|
|
169
|
+
Add to your Claude Desktop config (`~/Library/Application Support/Claude/claude_desktop_config.json` on macOS):
|
|
170
|
+
|
|
171
|
+
```json
|
|
172
|
+
{
|
|
173
|
+
"mcpServers": {
|
|
174
|
+
"my-app": {
|
|
175
|
+
"command": "mcp-connect",
|
|
176
|
+
"cwd": "/path/to/your/project"
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
#### Local Installation
|
|
183
|
+
|
|
184
|
+
```json
|
|
185
|
+
{
|
|
186
|
+
"mcpServers": {
|
|
187
|
+
"my-app": {
|
|
188
|
+
"command": "npx",
|
|
189
|
+
"args": ["mcp-connect"],
|
|
190
|
+
"cwd": "/path/to/your/project"
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
#### Alternative: Package Script
|
|
197
|
+
|
|
198
|
+
Add to your project's `package.json`:
|
|
199
|
+
|
|
200
|
+
```json
|
|
201
|
+
{
|
|
202
|
+
"scripts": {
|
|
203
|
+
"mcp": "mcp-connect"
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
Then in Claude Desktop config:
|
|
209
|
+
|
|
210
|
+
```json
|
|
211
|
+
{
|
|
212
|
+
"mcpServers": {
|
|
213
|
+
"my-app": {
|
|
214
|
+
"command": "npm",
|
|
215
|
+
"args": ["run", "mcp"],
|
|
216
|
+
"cwd": "/path/to/your/project"
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
```
|
|
221
|
+
|
|
222
|
+
### Other MCP Clients
|
|
223
|
+
|
|
224
|
+
Any MCP-compliant client can connect using the STDIO transport. The server implements the full MCP specification with:
|
|
225
|
+
|
|
226
|
+
- `tools/list` - List available tools
|
|
227
|
+
- `tools/call` - Execute tool functions
|
|
228
|
+
- Proper JSON-RPC 2.0 messaging
|
|
229
|
+
- Standard MCP lifecycle management
|
|
230
|
+
|
|
231
|
+
## 📁 Examples
|
|
232
|
+
|
|
233
|
+
### Todo App Example
|
|
234
|
+
|
|
235
|
+
```javascript
|
|
236
|
+
import { defineMCP } from "mcp-connect";
|
|
237
|
+
|
|
238
|
+
const todos = [{ id: 1, title: "Buy milk", completed: false }];
|
|
239
|
+
|
|
240
|
+
export default defineMCP({
|
|
241
|
+
name: "Todo App",
|
|
242
|
+
version: "1.0.0",
|
|
243
|
+
description: "Simple todo list management via MCP",
|
|
244
|
+
tools: [
|
|
245
|
+
// Simple tuple format
|
|
246
|
+
["getTodos", async () => todos],
|
|
247
|
+
|
|
248
|
+
// Object format with schema validation
|
|
249
|
+
{
|
|
250
|
+
name: "addTodo",
|
|
251
|
+
description: "Add a new todo item",
|
|
252
|
+
schema: {
|
|
253
|
+
type: "object",
|
|
254
|
+
properties: {
|
|
255
|
+
title: { type: "string", description: "Todo title" }
|
|
256
|
+
},
|
|
257
|
+
required: ["title"]
|
|
258
|
+
},
|
|
259
|
+
handler: async ({ title }) => {
|
|
260
|
+
const newTodo = { id: Date.now(), title, completed: false };
|
|
261
|
+
todos.push(newTodo);
|
|
262
|
+
return newTodo;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
]
|
|
266
|
+
});
|
|
267
|
+
```
|
|
268
|
+
|
|
269
|
+
Check out the [examples](./examples) directory for complete working examples.
|
|
270
|
+
|
|
271
|
+
## 🛠 Development
|
|
272
|
+
|
|
273
|
+
```bash
|
|
274
|
+
# Clone and install
|
|
275
|
+
git clone https://github.com/myat-kyaw-thu/MCP_Indigration_Package-NPM.git
|
|
276
|
+
cd mcp-connect
|
|
277
|
+
npm install
|
|
278
|
+
|
|
279
|
+
# Run example with debug logging
|
|
280
|
+
cd examples/todo-app
|
|
281
|
+
MCP_DEBUG=1 node ../../src/cli.js
|
|
282
|
+
|
|
283
|
+
# Test global installation locally
|
|
284
|
+
npm link
|
|
285
|
+
cd /path/to/test/project
|
|
286
|
+
mcp-connect
|
|
287
|
+
|
|
288
|
+
# Run tests
|
|
289
|
+
npm test
|
|
290
|
+
|
|
291
|
+
# Lint and format code
|
|
292
|
+
npm run lint
|
|
293
|
+
npm run format
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
## 🏗️ Architecture
|
|
297
|
+
|
|
298
|
+
- **Pure JavaScript** with JSDoc type annotations
|
|
299
|
+
- **ESM modules** with Node.js 18+ support
|
|
300
|
+
- **Express.js** HTTP server integration
|
|
301
|
+
- **Official MCP SDK** integration
|
|
302
|
+
- **Structured logging** with performance metrics
|
|
303
|
+
- **Comprehensive validation** with user-friendly errors
|
|
304
|
+
- **Pure MCP implementation** focused on STDIO transport
|
|
305
|
+
|
|
306
|
+
## 📄 License
|
|
307
|
+
|
|
308
|
+
MIT © [myat-kyaw-thu](https://github.com/myat-kyaw-thu)
|
package/package.json
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@myatkyawthu/mcp-connect",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Dead simple MCP (Model Context Protocol) server for exposing your app functions to AI agents",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.js",
|
|
7
|
+
"module": "./src/index.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./src/index.js",
|
|
11
|
+
"require": "./src/index.js"
|
|
12
|
+
}
|
|
13
|
+
},
|
|
14
|
+
"bin": {
|
|
15
|
+
"mcp-connect": "./src/cli.js"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"src",
|
|
19
|
+
"README.md",
|
|
20
|
+
"LICENSE"
|
|
21
|
+
],
|
|
22
|
+
"scripts": {
|
|
23
|
+
"start": "node src/cli.js",
|
|
24
|
+
"dev": "nodemon src/cli.js",
|
|
25
|
+
"test": "node --test",
|
|
26
|
+
"test:unit": "node --test tests/unit",
|
|
27
|
+
"test:integration": "node --test tests/integration",
|
|
28
|
+
"lint": "eslint src/",
|
|
29
|
+
"format": "prettier --write src/"
|
|
30
|
+
},
|
|
31
|
+
"keywords": [
|
|
32
|
+
"mcp",
|
|
33
|
+
"model-context-protocol",
|
|
34
|
+
"ai",
|
|
35
|
+
"agents",
|
|
36
|
+
"tools",
|
|
37
|
+
"claude",
|
|
38
|
+
"gpt",
|
|
39
|
+
"javascript",
|
|
40
|
+
"nodejs"
|
|
41
|
+
],
|
|
42
|
+
"author": "myat-kyaw-thu",
|
|
43
|
+
"license": "MIT",
|
|
44
|
+
"repository": {
|
|
45
|
+
"type": "git",
|
|
46
|
+
"url": "https://github.com/myat-kyaw-thu/MCP_Indigration_Package-NPM.git"
|
|
47
|
+
},
|
|
48
|
+
"bugs": {
|
|
49
|
+
"url": "https://github.com/myat-kyaw-thu/MCP_Indigration_Package-NPM/issues"
|
|
50
|
+
},
|
|
51
|
+
"homepage": "https://github.com/myat-kyaw-thu/MCP_Indigration_Package-NPM#readme",
|
|
52
|
+
"engines": {
|
|
53
|
+
"node": ">=18.0.0"
|
|
54
|
+
},
|
|
55
|
+
"dependencies": {
|
|
56
|
+
"@modelcontextprotocol/sdk": "^0.5.0"
|
|
57
|
+
},
|
|
58
|
+
"devDependencies": {
|
|
59
|
+
"nodemon": "^3.0.0",
|
|
60
|
+
"eslint": "^8.0.0",
|
|
61
|
+
"prettier": "^3.0.0"
|
|
62
|
+
}
|
|
63
|
+
}
|
package/src/cli.js
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { existsSync } from "fs";
|
|
4
|
+
import { resolve } from "path";
|
|
5
|
+
import { defineMCP } from "./defineMCP.js";
|
|
6
|
+
import { MCPConnectServer } from "./server/mcpServer.js";
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* CLI entry point for mcp-connect
|
|
10
|
+
* Uses MCP SDK with STDIO transport or Express.js for HTTP transport
|
|
11
|
+
*/
|
|
12
|
+
async function main() {
|
|
13
|
+
try {
|
|
14
|
+
console.error("Starting MCP-Connect CLI...");
|
|
15
|
+
|
|
16
|
+
// Check if config file path is provided as argument
|
|
17
|
+
const configArg = process.argv[2];
|
|
18
|
+
/** @type {string|null} */
|
|
19
|
+
let configPath = null;
|
|
20
|
+
|
|
21
|
+
if (configArg) {
|
|
22
|
+
// Config file path provided as argument
|
|
23
|
+
const fullPath = resolve(configArg);
|
|
24
|
+
if (existsSync(fullPath)) {
|
|
25
|
+
configPath = fullPath;
|
|
26
|
+
} else {
|
|
27
|
+
console.error(`Config file not found: ${configArg}`);
|
|
28
|
+
process.exit(1);
|
|
29
|
+
}
|
|
30
|
+
} else {
|
|
31
|
+
// Look for config file in current directory (prioritize .js and .mjs)
|
|
32
|
+
const configPaths = ["mcp.config.js", "mcp.config.mjs", "mcp.config.ts"];
|
|
33
|
+
|
|
34
|
+
for (const path of configPaths) {
|
|
35
|
+
const fullPath = resolve(process.cwd(), path);
|
|
36
|
+
if (existsSync(fullPath)) {
|
|
37
|
+
configPath = fullPath;
|
|
38
|
+
break;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (!configPath) {
|
|
44
|
+
console.error("No MCP config file found. Create mcp.config.js in your project root.");
|
|
45
|
+
console.error("Example config:");
|
|
46
|
+
console.error(`
|
|
47
|
+
import { defineMCP } from "mcp-connect"
|
|
48
|
+
|
|
49
|
+
export default defineMCP({
|
|
50
|
+
name: "My App",
|
|
51
|
+
version: "1.0.0",
|
|
52
|
+
tools: [
|
|
53
|
+
["hello", async ({ name }) => \`Hello \${name}!\`]
|
|
54
|
+
]
|
|
55
|
+
})
|
|
56
|
+
`);
|
|
57
|
+
process.exit(1);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
console.error(`Loading config from: ${configPath}`);
|
|
61
|
+
|
|
62
|
+
let configModule;
|
|
63
|
+
try {
|
|
64
|
+
// Handle TypeScript config files
|
|
65
|
+
if (configPath.endsWith(".ts")) {
|
|
66
|
+
console.error("TypeScript config detected. For Node.js compatibility:");
|
|
67
|
+
console.error("1. Rename mcp.config.ts to mcp.config.js and convert to JavaScript");
|
|
68
|
+
console.error("2. Or install tsx: npm install tsx");
|
|
69
|
+
console.error("3. Then run with: npx tsx src/cli.js");
|
|
70
|
+
console.error("");
|
|
71
|
+
console.error("JavaScript example:");
|
|
72
|
+
console.error(`
|
|
73
|
+
// mcp.config.js
|
|
74
|
+
import { defineMCP } from "mcp-connect";
|
|
75
|
+
|
|
76
|
+
export default defineMCP({
|
|
77
|
+
name: "My App",
|
|
78
|
+
version: "1.0.0",
|
|
79
|
+
tools: [
|
|
80
|
+
["hello", async ({ name }) => \`Hello \${name}!\`]
|
|
81
|
+
]
|
|
82
|
+
});
|
|
83
|
+
`);
|
|
84
|
+
process.exit(1);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
// Import config file using Node.js ES modules
|
|
88
|
+
const fileUrl = configPath.startsWith('/')
|
|
89
|
+
? `file://${configPath}`
|
|
90
|
+
: `file:///${configPath.replace(/\\/g, '/')}`;
|
|
91
|
+
|
|
92
|
+
configModule = await import(fileUrl);
|
|
93
|
+
} catch (error) {
|
|
94
|
+
console.error("Failed to load config file:");
|
|
95
|
+
if (error instanceof Error) {
|
|
96
|
+
console.error("Error:", error.message);
|
|
97
|
+
|
|
98
|
+
if (error.message.includes("Cannot resolve") || error.message.includes("MODULE_NOT_FOUND")) {
|
|
99
|
+
console.error("Make sure 'mcp-connect' is installed: npm install mcp-connect");
|
|
100
|
+
} else if (error.message.includes("SyntaxError")) {
|
|
101
|
+
console.error("Config file has syntax errors. Check your JavaScript syntax.");
|
|
102
|
+
} else if (error.message.includes("ERR_MODULE_NOT_FOUND")) {
|
|
103
|
+
console.error("Module import error. Check your import paths in the config file.");
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
process.exit(1);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const config = configModule.default;
|
|
110
|
+
|
|
111
|
+
if (!config) {
|
|
112
|
+
console.error("Config file must export a default configuration");
|
|
113
|
+
console.error("Make sure your config has: export default defineMCP({...})");
|
|
114
|
+
process.exit(1);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// Validate config using defineMCP (in case user didn't use it)
|
|
118
|
+
let validatedConfig;
|
|
119
|
+
try {
|
|
120
|
+
validatedConfig = typeof config === "function" ? config : defineMCP(config);
|
|
121
|
+
} catch (error) {
|
|
122
|
+
console.error("Configuration validation failed:", error);
|
|
123
|
+
process.exit(1);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
// Create and start MCP server
|
|
127
|
+
const server = new MCPConnectServer(validatedConfig);
|
|
128
|
+
await server.start();
|
|
129
|
+
|
|
130
|
+
// Handle graceful shutdown
|
|
131
|
+
process.on("SIGINT", async () => {
|
|
132
|
+
console.error("Shutting down...");
|
|
133
|
+
try {
|
|
134
|
+
await server.stop();
|
|
135
|
+
} catch (error) {
|
|
136
|
+
console.error("Error during shutdown:", error);
|
|
137
|
+
}
|
|
138
|
+
process.exit(0);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
process.on("SIGTERM", async () => {
|
|
142
|
+
console.error("Shutting down...");
|
|
143
|
+
try {
|
|
144
|
+
await server.stop();
|
|
145
|
+
} catch (error) {
|
|
146
|
+
console.error("Error during shutdown:", error);
|
|
147
|
+
}
|
|
148
|
+
process.exit(0);
|
|
149
|
+
});
|
|
150
|
+
} catch (error) {
|
|
151
|
+
console.error("Failed to start MCP server:", error);
|
|
152
|
+
process.exit(1);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// Run CLI if this file is executed directly
|
|
157
|
+
// Multiple checks to ensure main() runs when CLI is executed
|
|
158
|
+
const isMainModule = process.argv[1] && (
|
|
159
|
+
import.meta.url === `file://${process.argv[1]}` ||
|
|
160
|
+
import.meta.url.endsWith(process.argv[1]) ||
|
|
161
|
+
process.argv[1].endsWith('cli.js') ||
|
|
162
|
+
process.argv[1].includes('mcp-connect')
|
|
163
|
+
);
|
|
164
|
+
|
|
165
|
+
if (isMainModule) {
|
|
166
|
+
main().catch((error) => {
|
|
167
|
+
console.error("CLI startup failed:", error);
|
|
168
|
+
process.exit(1);
|
|
169
|
+
});
|
|
170
|
+
}
|
package/src/defineMCP.js
ADDED
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
import { formatValidationErrors, validateConfig } from "./utils/configValidation.js";
|
|
2
|
+
import { logger } from "./utils/logger.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Define MCP configuration with tools and server settings
|
|
6
|
+
* This is the main user-facing API that converts simple tool definitions
|
|
7
|
+
* into MCP-compliant format with comprehensive validation
|
|
8
|
+
*
|
|
9
|
+
* @param {Object} config - Configuration object
|
|
10
|
+
* @param {string} config.name - Server name
|
|
11
|
+
* @param {string} config.version - Server version
|
|
12
|
+
* @param {string} [config.description] - Server description
|
|
13
|
+
* @param {import('./types/mcp.js').ToolDefinition[]} config.tools - Tool definitions
|
|
14
|
+
* @returns {import('./types/mcp.js').MCPConfig} MCP configuration object
|
|
15
|
+
* @throws {Error} If configuration is invalid
|
|
16
|
+
*/
|
|
17
|
+
export function defineMCP(config) {
|
|
18
|
+
// Comprehensive configuration validation
|
|
19
|
+
const validationResult = validateConfig(config);
|
|
20
|
+
|
|
21
|
+
if (!validationResult.isValid) {
|
|
22
|
+
const errorMessage = formatValidationErrors(validationResult);
|
|
23
|
+
logger.error("Configuration validation failed", errorMessage);
|
|
24
|
+
throw new Error(`Invalid MCP configuration:\n${errorMessage}`);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// Log warnings if any
|
|
28
|
+
if (validationResult.warnings.length > 0) {
|
|
29
|
+
const warningMessage = formatValidationErrors({ isValid: true, errors: [], warnings: validationResult.warnings });
|
|
30
|
+
logger.warn("Configuration warnings", warningMessage);
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// Convert validated tool definitions to MCP format
|
|
34
|
+
/** @type {import('./types/mcp.js').MCPTool[]} */
|
|
35
|
+
const mcpTools = config.tools.map((tool) => {
|
|
36
|
+
if (Array.isArray(tool)) {
|
|
37
|
+
// Handle [name, function] format
|
|
38
|
+
const [name, handler] = tool;
|
|
39
|
+
return {
|
|
40
|
+
name: name.trim(),
|
|
41
|
+
description: `Tool: ${name}`,
|
|
42
|
+
inputSchema: {
|
|
43
|
+
type: "object",
|
|
44
|
+
properties: {},
|
|
45
|
+
additionalProperties: true,
|
|
46
|
+
},
|
|
47
|
+
handler,
|
|
48
|
+
};
|
|
49
|
+
} else {
|
|
50
|
+
// Handle object format
|
|
51
|
+
const { name, handler, description, schema } = tool;
|
|
52
|
+
return {
|
|
53
|
+
name: name.trim(),
|
|
54
|
+
description: description || `Tool: ${name}`,
|
|
55
|
+
inputSchema: schema || {
|
|
56
|
+
type: "object",
|
|
57
|
+
properties: {},
|
|
58
|
+
additionalProperties: true,
|
|
59
|
+
},
|
|
60
|
+
handler,
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
// Return MCP-compliant config
|
|
66
|
+
return {
|
|
67
|
+
name: config.name,
|
|
68
|
+
version: config.version,
|
|
69
|
+
description: config.description,
|
|
70
|
+
tools: mcpTools,
|
|
71
|
+
};
|
|
72
|
+
}
|
package/src/index.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP-Connect - Dead simple MCP (Model Context Protocol) server for exposing your app functions to AI agents
|
|
3
|
+
*
|
|
4
|
+
* Main entry point - exports public API
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```javascript
|
|
8
|
+
* import { defineMCP } from 'mcp-connect';
|
|
9
|
+
*
|
|
10
|
+
* export default defineMCP({
|
|
11
|
+
* name: 'My App',
|
|
12
|
+
* version: '1.0.0',
|
|
13
|
+
* tools: [
|
|
14
|
+
* ['hello', async ({ name }) => `Hello ${name}!`]
|
|
15
|
+
* ]
|
|
16
|
+
* });
|
|
17
|
+
* ```
|
|
18
|
+
*/
|
|
19
|
+
|
|
20
|
+
// Main API exports
|
|
21
|
+
export { defineMCP } from "./defineMCP.js";
|
|
22
|
+
export { MCPConnectServer } from "./server/mcpServer.js";
|
|
23
|
+
|
|
24
|
+
// Type validation utilities (for runtime type checking)
|
|
25
|
+
export {
|
|
26
|
+
isValidMCPConfig, isValidMCPTool, isValidToolDefinition
|
|
27
|
+
} from "./types/mcp.js";
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* @typedef {import('./types/mcp.js').MCPConfig} MCPConfig
|
|
31
|
+
* @typedef {import('./types/mcp.js').MCPTool} MCPTool
|
|
32
|
+
* @typedef {import('./types/mcp.js').ToolDefinition} ToolDefinition
|
|
33
|
+
* @typedef {import('./types/mcp.js').MCPServerOptions} MCPServerOptions
|
|
34
|
+
* @typedef {import('./types/mcp.js').ToolFunction} ToolFunction
|
|
35
|
+
*/
|