create-mcp-kit 0.0.6 → 0.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.
- package/README.md +242 -50
- package/dist/index.js +21 -24
- package/package.json +8 -3
- package/template/client-js/.env.hbs +0 -0
- package/template/client-js/.gitignore.hbs +112 -0
- package/template/client-js/.nvmrc.hbs +1 -0
- package/template/client-js/.prettierrc.hbs +13 -0
- package/template/client-js/LICENSE.hbs +21 -0
- package/template/client-js/_github/workflows/build.yml.hbs +39 -0
- package/template/client-js/_github/workflows/npm-publish.yml.hbs +41 -0
- package/template/client-js/_husky/commit-msg.hbs +3 -0
- package/template/client-js/_husky/pre-commit.hbs +3 -0
- package/template/client-js/changelog-option.js.hbs +89 -0
- package/template/client-js/commitlint.config.js.hbs +27 -0
- package/template/client-js/eslint.config.js.hbs +48 -0
- package/template/client-js/jsconfig.json.hbs +21 -0
- package/template/client-js/lint-staged.config.js.hbs +5 -0
- package/template/client-js/package.json.hbs +66 -0
- package/template/client-js/scripts/base.js.hbs +65 -0
- package/template/client-js/scripts/build.js.hbs +4 -0
- package/template/client-js/scripts/dev.js.hbs +7 -0
- package/template/client-js/src/index.js.hbs +153 -0
- package/template/client-js/tests/index.test.js.hbs +22 -0
- package/template/{server-js/tests/utils.js → client-js/tests/utils.js.hbs} +2 -0
- package/template/client-js/vitest.config.js.hbs +27 -0
- package/template/client-js/vitest.global.js.hbs +21 -0
- package/template/client-ts/.env.hbs +0 -0
- package/template/client-ts/.gitignore.hbs +112 -0
- package/template/client-ts/.nvmrc.hbs +1 -0
- package/template/client-ts/.prettierrc.hbs +13 -0
- package/template/client-ts/LICENSE.hbs +21 -0
- package/template/client-ts/_github/workflows/build.yml.hbs +39 -0
- package/template/client-ts/_github/workflows/npm-publish.yml.hbs +41 -0
- package/template/client-ts/_husky/commit-msg.hbs +3 -0
- package/template/client-ts/_husky/pre-commit.hbs +3 -0
- package/template/client-ts/changelog-option.js.hbs +89 -0
- package/template/client-ts/commitlint.config.js.hbs +27 -0
- package/template/client-ts/eslint.config.js.hbs +55 -0
- package/template/client-ts/lint-staged.config.js.hbs +5 -0
- package/template/client-ts/package.json.hbs +73 -0
- package/template/client-ts/scripts/base.js.hbs +65 -0
- package/template/client-ts/scripts/build.js.hbs +4 -0
- package/template/client-ts/scripts/dev.js.hbs +7 -0
- package/template/client-ts/src/index.ts.hbs +153 -0
- package/template/client-ts/tests/index.test.ts.hbs +22 -0
- package/template/{server-ts/tests/utils.ts → client-ts/tests/utils.ts.hbs} +2 -0
- package/template/client-ts/tsconfig.json.hbs +21 -0
- package/template/client-ts/vitest.config.ts.hbs +27 -0
- package/template/client-ts/vitest.global.ts.hbs +21 -0
- package/template/server-js/jsconfig.json.hbs +5 -1
- package/template/server-js/package.json.hbs +5 -5
- package/template/server-js/tests/utils.js.hbs +18 -0
- package/template/server-js/vitest.config.js.hbs +5 -0
- package/template/server-js/vitest.global.js.hbs +1 -1
- package/template/server-ts/package.json.hbs +9 -8
- package/template/server-ts/tests/utils.ts.hbs +22 -0
- package/template/server-ts/tsconfig.json.hbs +5 -1
- package/template/server-ts/vitest.config.ts.hbs +5 -0
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{{#if (includes plugins 'github-action')}}
|
|
2
|
+
name: npm-publish
|
|
3
|
+
|
|
4
|
+
on:
|
|
5
|
+
release:
|
|
6
|
+
types: [created]
|
|
7
|
+
|
|
8
|
+
jobs:
|
|
9
|
+
build:
|
|
10
|
+
if: github.repository == '{{projectName}}'
|
|
11
|
+
runs-on: ubuntu-latest
|
|
12
|
+
permissions:
|
|
13
|
+
contents: read
|
|
14
|
+
id-token: write
|
|
15
|
+
steps:
|
|
16
|
+
- name: Checkout
|
|
17
|
+
uses: actions/checkout@v4
|
|
18
|
+
with:
|
|
19
|
+
fetch-depth: 0
|
|
20
|
+
|
|
21
|
+
- name: Install Node
|
|
22
|
+
uses: actions/setup-node@v4
|
|
23
|
+
with:
|
|
24
|
+
node-version: 22
|
|
25
|
+
registry-url: https://registry.npmjs.org/
|
|
26
|
+
cache: npm
|
|
27
|
+
|
|
28
|
+
- name: Install Package
|
|
29
|
+
run: npm i
|
|
30
|
+
|
|
31
|
+
- name: Lint
|
|
32
|
+
run: npm run lint
|
|
33
|
+
|
|
34
|
+
- name: Build Package
|
|
35
|
+
run: npm run build
|
|
36
|
+
|
|
37
|
+
- name: Publish NPM Package
|
|
38
|
+
run: npm publish --provenance --access public
|
|
39
|
+
env:
|
|
40
|
+
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
|
|
41
|
+
{{/if}}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
{{#if (includes plugins 'changelog')}}
|
|
2
|
+
import compareFunc from 'compare-func'
|
|
3
|
+
|
|
4
|
+
export default {
|
|
5
|
+
writerOpts: {
|
|
6
|
+
transform: (commit, context) => {
|
|
7
|
+
let discard = true
|
|
8
|
+
const issues = []
|
|
9
|
+
|
|
10
|
+
const newCommit = {
|
|
11
|
+
...commit,
|
|
12
|
+
notes: commit.notes.map(note => ({
|
|
13
|
+
...note,
|
|
14
|
+
title: 'BREAKING CHANGES',
|
|
15
|
+
})),
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
if (newCommit.notes.length > 0) {
|
|
19
|
+
discard = false
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const typeMap = {
|
|
23
|
+
feat: '✨ Features | 新功能',
|
|
24
|
+
fix: '🐛 Bug Fixes | Bug 修复',
|
|
25
|
+
perf: '⚡ Performance Improvements | 性能优化',
|
|
26
|
+
revert: '⏪ Reverts | 回退',
|
|
27
|
+
refactor: '♻ Code Refactoring | 代码重构',
|
|
28
|
+
test: '✅ Tests | 测试',
|
|
29
|
+
build: '👷 Build System | 构建',
|
|
30
|
+
chore: '🎫 Chores | 其他更新',
|
|
31
|
+
style: '💄 Styles | 风格',
|
|
32
|
+
ci: '🔧 Continuous Integration | CI 配置',
|
|
33
|
+
docs: '📝 Documentation | 文档',
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (typeMap[newCommit.type]) {
|
|
37
|
+
newCommit.type = typeMap[newCommit.type]
|
|
38
|
+
} else if (newCommit.type === 'revert' || newCommit.revert) {
|
|
39
|
+
newCommit.type = typeMap['revert']
|
|
40
|
+
} else if (discard) {
|
|
41
|
+
return
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if (newCommit.scope === '*') {
|
|
45
|
+
newCommit.scope = ''
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if (typeof newCommit.hash === 'string') {
|
|
49
|
+
newCommit.shortHash = newCommit.hash.substring(0, 7)
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (typeof newCommit.subject === 'string') {
|
|
53
|
+
let url = context.repository ? `${context.host}/${context.owner}/${context.repository}` : context.repoUrl
|
|
54
|
+
|
|
55
|
+
if (url) {
|
|
56
|
+
url = `${url}/issues/`
|
|
57
|
+
newCommit.subject = newCommit.subject.replace(/#([0-9]+)/g, (_, issue) => {
|
|
58
|
+
issues.push(issue)
|
|
59
|
+
return `[#${issue}](${url}${issue})`
|
|
60
|
+
})
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
if (context.host) {
|
|
64
|
+
newCommit.subject = newCommit.subject.replace(/\B@([a-z0-9](?:-?[a-z0-9/]){0,38})/g, (_, username) => {
|
|
65
|
+
if (username.includes('/')) {
|
|
66
|
+
return `@${username}`
|
|
67
|
+
}
|
|
68
|
+
return `[@${username}](${context.host}/${username})`
|
|
69
|
+
})
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
newCommit.references = newCommit.references.filter(reference => {
|
|
74
|
+
if (issues.indexOf(reference.issue) === -1) {
|
|
75
|
+
return true
|
|
76
|
+
}
|
|
77
|
+
return false
|
|
78
|
+
})
|
|
79
|
+
|
|
80
|
+
return newCommit
|
|
81
|
+
},
|
|
82
|
+
groupBy: 'type',
|
|
83
|
+
commitGroupsSort: 'title',
|
|
84
|
+
commitsSort: ['scope', 'subject'],
|
|
85
|
+
noteGroupsSort: 'title',
|
|
86
|
+
notesSort: compareFunc,
|
|
87
|
+
},
|
|
88
|
+
}
|
|
89
|
+
{{/if}}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{{#if (includes plugins 'commitlint')}}
|
|
2
|
+
export default {
|
|
3
|
+
extends: ['@commitlint/config-conventional'],
|
|
4
|
+
rules: {
|
|
5
|
+
'type-enum': [
|
|
6
|
+
2,
|
|
7
|
+
'always',
|
|
8
|
+
[
|
|
9
|
+
'feat',
|
|
10
|
+
'fix',
|
|
11
|
+
'docs',
|
|
12
|
+
'style',
|
|
13
|
+
'refactor',
|
|
14
|
+
'perf',
|
|
15
|
+
'test',
|
|
16
|
+
'build',
|
|
17
|
+
'ci',
|
|
18
|
+
'chore',
|
|
19
|
+
'revert',
|
|
20
|
+
'build',
|
|
21
|
+
'release',
|
|
22
|
+
],
|
|
23
|
+
],
|
|
24
|
+
'subject-case': [0],
|
|
25
|
+
},
|
|
26
|
+
}
|
|
27
|
+
{{/if}}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
{{#if (includes plugins 'style')}}
|
|
2
|
+
import importPlugin from 'eslint-plugin-import'
|
|
3
|
+
import prettierPlugin from 'eslint-plugin-prettier'
|
|
4
|
+
import globals from 'globals'
|
|
5
|
+
|
|
6
|
+
export default [
|
|
7
|
+
{
|
|
8
|
+
ignores: ['**/build', '**/node_modules', '**/.*', '.husky/'],
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
{{#if (includes plugins 'vitest')}}
|
|
12
|
+
files: ['src/**/*.js', 'tests/**/*.js'],
|
|
13
|
+
{{else}}
|
|
14
|
+
files: ['src/**/*.js'],
|
|
15
|
+
{{/if}}
|
|
16
|
+
},
|
|
17
|
+
{
|
|
18
|
+
languageOptions: {
|
|
19
|
+
parserOptions: {
|
|
20
|
+
ecmaVersion: 'latest',
|
|
21
|
+
sourceType: 'module',
|
|
22
|
+
},
|
|
23
|
+
globals: {
|
|
24
|
+
...globals.es2022,
|
|
25
|
+
...globals.node,
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
plugins: {
|
|
29
|
+
import: importPlugin,
|
|
30
|
+
prettier: prettierPlugin,
|
|
31
|
+
},
|
|
32
|
+
rules: {
|
|
33
|
+
'prettier/prettier': 'error',
|
|
34
|
+
'no-console': 'off',
|
|
35
|
+
semi: ['warn', 'never'],
|
|
36
|
+
quotes: ['warn', 'single'],
|
|
37
|
+
'no-unused-vars': 'off',
|
|
38
|
+
},
|
|
39
|
+
settings: {
|
|
40
|
+
'import/resolver': {
|
|
41
|
+
node: {
|
|
42
|
+
extensions: ['.js'],
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
},
|
|
47
|
+
]
|
|
48
|
+
{{/if}}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "bundler",
|
|
6
|
+
"outDir": "./build",
|
|
7
|
+
"strict": true,
|
|
8
|
+
"esModuleInterop": true,
|
|
9
|
+
"skipLibCheck": true,
|
|
10
|
+
"forceConsistentCasingInFileNames": true,
|
|
11
|
+
"paths": {
|
|
12
|
+
"@/*": ["./src/*"]
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
{{#if (includes plugins 'vitest')}}
|
|
16
|
+
"include": ["src", "tests", "vitest.config.js", "vitest.global.js"],
|
|
17
|
+
{{else}}
|
|
18
|
+
"include": ["src"],
|
|
19
|
+
{{/if}}
|
|
20
|
+
"exclude": ["node_modules"]
|
|
21
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "{{projectName}}",
|
|
3
|
+
"version": "0.0.0",
|
|
4
|
+
"description": "{{projectName}}",
|
|
5
|
+
"author": "zhensherlock",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"files": [
|
|
9
|
+
"build",
|
|
10
|
+
"LICENSE",
|
|
11
|
+
"README.md"
|
|
12
|
+
],
|
|
13
|
+
"main": "build/index.js",
|
|
14
|
+
"scripts": {
|
|
15
|
+
{{#if (includes plugins 'style')}}
|
|
16
|
+
"lint": "npx eslint \"src/**/*.js\"",
|
|
17
|
+
{{/if}}
|
|
18
|
+
"build": "tsc --noEmit && cross-env NODE_ENV=production node scripts/build.js",
|
|
19
|
+
"dev": "cross-env NODE_ENV=local node scripts/dev.js",
|
|
20
|
+
{{#if (includes plugins 'vitest')}}
|
|
21
|
+
"test": "vitest run",
|
|
22
|
+
"coverage": "rimraf coverage && vitest run --coverage",
|
|
23
|
+
{{/if}}
|
|
24
|
+
{{#if (includes plugins 'changelog')}}
|
|
25
|
+
"changelog": "conventional-changelog -p angular -i CHANGELOG.md -s -r 0 -n changelog-option.js",
|
|
26
|
+
{{/if}}
|
|
27
|
+
"prepare": "husky"
|
|
28
|
+
},
|
|
29
|
+
"dependencies": {
|
|
30
|
+
"@modelcontextprotocol/sdk": "^1.17.4",
|
|
31
|
+
"dotenv": "^17.2.1"
|
|
32
|
+
},
|
|
33
|
+
"devDependencies": {
|
|
34
|
+
{{#if (includes plugins 'commitlint')}}
|
|
35
|
+
"@commitlint/cli": "^19.8.1",
|
|
36
|
+
"@commitlint/config-conventional": "^19.8.1",
|
|
37
|
+
{{/if}}
|
|
38
|
+
{{#if (includes plugins 'vitest')}}
|
|
39
|
+
"@vitest/coverage-v8": "^3.2.4",
|
|
40
|
+
{{/if}}
|
|
41
|
+
{{#if (includes plugins 'changelog')}}
|
|
42
|
+
"compare-func": "^2.0.0",
|
|
43
|
+
{{/if}}
|
|
44
|
+
{{#if (includes plugins 'changelog')}}
|
|
45
|
+
"conventional-changelog-angular": "^8.0.0",
|
|
46
|
+
"conventional-changelog-cli": "^5.0.0",
|
|
47
|
+
{{/if}}
|
|
48
|
+
"cross-env": "^10.0.0",
|
|
49
|
+
"esbuild": "^0.25.9",
|
|
50
|
+
{{#if (includes plugins 'style')}}
|
|
51
|
+
"eslint": "^9.34.0",
|
|
52
|
+
"eslint-plugin-import": "^2.32.0",
|
|
53
|
+
"eslint-plugin-prettier": "^5.5.4",
|
|
54
|
+
"globals": "^16.3.0",
|
|
55
|
+
{{/if}}
|
|
56
|
+
"husky": "^9.1.7",
|
|
57
|
+
{{#if (includes plugins 'style')}}
|
|
58
|
+
"lint-staged": "^16.1.5",
|
|
59
|
+
"prettier": "^3.6.2",
|
|
60
|
+
{{/if}}
|
|
61
|
+
{{#if (includes plugins 'vitest')}}
|
|
62
|
+
"vitest": "^3.2.4",
|
|
63
|
+
{{/if}}
|
|
64
|
+
"rimraf": "^6.0.1"
|
|
65
|
+
}
|
|
66
|
+
}
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import path from 'path'
|
|
2
|
+
import { fileURLToPath } from 'url'
|
|
3
|
+
import { spawn } from 'child_process'
|
|
4
|
+
import { rimraf } from 'rimraf'
|
|
5
|
+
|
|
6
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
|
7
|
+
const isProd = process.env.NODE_ENV === 'production'
|
|
8
|
+
const isDev = process.env.NODE_ENV === 'local'
|
|
9
|
+
let nodeProcess = null
|
|
10
|
+
|
|
11
|
+
{{#if (or (includes transports 'streamable') (includes transports 'sse'))}}
|
|
12
|
+
if (isDev) {
|
|
13
|
+
spawn('npx', ['-y', '@my-mcp-hub/node-mcp-server', 'web'], {
|
|
14
|
+
stdio: 'inherit',
|
|
15
|
+
})
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
{{/if}}
|
|
19
|
+
/** @type {import('esbuild').BuildOptions} */
|
|
20
|
+
export const config = {
|
|
21
|
+
entryPoints: [path.resolve(__dirname, '../src/index.js')],
|
|
22
|
+
outfile: path.resolve(__dirname, '../build/index.js'),
|
|
23
|
+
format: 'esm',
|
|
24
|
+
bundle: true,
|
|
25
|
+
sourcemap: isDev,
|
|
26
|
+
minify: isProd,
|
|
27
|
+
platform: 'node',
|
|
28
|
+
external: ['dotenv', 'timers/promises', '@modelcontextprotocol/sdk'],
|
|
29
|
+
alias: {
|
|
30
|
+
'@': path.resolve(__dirname, '../src'),
|
|
31
|
+
},
|
|
32
|
+
plugins: [
|
|
33
|
+
{
|
|
34
|
+
name: 'build-plugin',
|
|
35
|
+
setup(build) {
|
|
36
|
+
build.onStart(async result => {
|
|
37
|
+
await before(result)
|
|
38
|
+
})
|
|
39
|
+
build.onEnd(async result => {
|
|
40
|
+
await after(result)
|
|
41
|
+
})
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
],
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const before = async () => {
|
|
48
|
+
await rimraf('build')
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
const after = async result => {
|
|
52
|
+
if (isDev) {
|
|
53
|
+
if (result.errors.length === 0) {
|
|
54
|
+
console.log('✅ Rebuild succeeded')
|
|
55
|
+
} else {
|
|
56
|
+
console.error('❌ Rebuild failed')
|
|
57
|
+
}
|
|
58
|
+
if (nodeProcess) {
|
|
59
|
+
nodeProcess.kill('SIGINT')
|
|
60
|
+
}
|
|
61
|
+
nodeProcess = spawn('node', ['./src/index.js'], {
|
|
62
|
+
stdio: 'inherit',
|
|
63
|
+
})
|
|
64
|
+
}
|
|
65
|
+
}
|
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import 'dotenv/config'
|
|
2
|
+
import { setTimeout as sleep } from 'timers/promises'
|
|
3
|
+
import { Client } from '@modelcontextprotocol/sdk/client/index.js'
|
|
4
|
+
{{#if (includes transports 'stdio')}}
|
|
5
|
+
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js'
|
|
6
|
+
{{/if}}
|
|
7
|
+
{{#if (includes transports 'streamable')}}
|
|
8
|
+
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js'
|
|
9
|
+
{{/if}}
|
|
10
|
+
{{#if (includes transports 'sse')}}
|
|
11
|
+
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js'
|
|
12
|
+
{{/if}}
|
|
13
|
+
{{#if (includes transports 'stdio')}}
|
|
14
|
+
|
|
15
|
+
export async function testStdioTransport() {
|
|
16
|
+
console.log('start stdio client transport')
|
|
17
|
+
const client = new Client({
|
|
18
|
+
name: 'mcp-stdio-client',
|
|
19
|
+
version: '1.0.0',
|
|
20
|
+
})
|
|
21
|
+
|
|
22
|
+
const stdioClientTransport = new StdioClientTransport({
|
|
23
|
+
command: 'npx',
|
|
24
|
+
args: ['-y', '@my-mcp-hub/node-mcp-server'],
|
|
25
|
+
env: {
|
|
26
|
+
...process.env,
|
|
27
|
+
},
|
|
28
|
+
})
|
|
29
|
+
|
|
30
|
+
try {
|
|
31
|
+
await client.connect(stdioClientTransport)
|
|
32
|
+
console.log('stdio client transport connected')
|
|
33
|
+
|
|
34
|
+
const { tools } = await client.listTools()
|
|
35
|
+
console.log(tools.map(item => item.name))
|
|
36
|
+
console.log('stdio client transport tools listed')
|
|
37
|
+
|
|
38
|
+
const callResult = await client.callTool({
|
|
39
|
+
name: 'GetData',
|
|
40
|
+
arguments: {
|
|
41
|
+
keyword: 'Hello',
|
|
42
|
+
},
|
|
43
|
+
})
|
|
44
|
+
console.log(callResult.content)
|
|
45
|
+
console.log('stdio client transport call tool done')
|
|
46
|
+
} catch (error) {
|
|
47
|
+
console.error('stdio transport error:', error)
|
|
48
|
+
} finally {
|
|
49
|
+
await client.close()
|
|
50
|
+
console.log('stdio client transport closed')
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
console.log('stdio client transport done\n\n')
|
|
54
|
+
await sleep(1000)
|
|
55
|
+
}
|
|
56
|
+
{{/if}}
|
|
57
|
+
{{#if (includes transports 'streamable')}}
|
|
58
|
+
|
|
59
|
+
export async function testStreamableHttpTransport() {
|
|
60
|
+
console.log('start streamable http client transport')
|
|
61
|
+
const client = new Client({
|
|
62
|
+
name: 'mcp-http-client',
|
|
63
|
+
version: '1.0.0',
|
|
64
|
+
})
|
|
65
|
+
|
|
66
|
+
const streamableBaseUrl = new URL('http://localhost:8401/mcp')
|
|
67
|
+
const streamableClientTransport = new StreamableHTTPClientTransport(streamableBaseUrl)
|
|
68
|
+
|
|
69
|
+
try {
|
|
70
|
+
await client.connect(streamableClientTransport)
|
|
71
|
+
console.log('streamable http client transport connected')
|
|
72
|
+
|
|
73
|
+
const { tools } = await client.listTools()
|
|
74
|
+
console.log(tools.map(item => item.name))
|
|
75
|
+
console.log('streamable http client transport tools listed')
|
|
76
|
+
|
|
77
|
+
const callResult = await client.callTool({
|
|
78
|
+
name: 'GetData',
|
|
79
|
+
arguments: {
|
|
80
|
+
keyword: 'Hello',
|
|
81
|
+
},
|
|
82
|
+
})
|
|
83
|
+
console.log(callResult.content)
|
|
84
|
+
console.log('streamable http client transport call tool done')
|
|
85
|
+
} catch (error) {
|
|
86
|
+
console.error('streamable http transport error:', error)
|
|
87
|
+
} finally {
|
|
88
|
+
await client.close()
|
|
89
|
+
console.log('streamable http client transport closed')
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
console.log('streamable http client transport done\n\n')
|
|
93
|
+
await sleep(1000)
|
|
94
|
+
}
|
|
95
|
+
{{/if}}
|
|
96
|
+
{{#if (includes transports 'sse')}}
|
|
97
|
+
|
|
98
|
+
export async function testSSETransport() {
|
|
99
|
+
console.log('start sse client transport')
|
|
100
|
+
const client = new Client({
|
|
101
|
+
name: 'mcp-sse-client',
|
|
102
|
+
version: '1.0.0',
|
|
103
|
+
})
|
|
104
|
+
|
|
105
|
+
const sseBaseUrl = new URL('http://localhost:8401/sse')
|
|
106
|
+
const sseClientTransport = new SSEClientTransport(sseBaseUrl)
|
|
107
|
+
|
|
108
|
+
try {
|
|
109
|
+
await client.connect(sseClientTransport)
|
|
110
|
+
console.log('sse client transport connected')
|
|
111
|
+
|
|
112
|
+
const { tools } = await client.listTools()
|
|
113
|
+
console.log(tools.map(item => item.name))
|
|
114
|
+
console.log('sse client transport tools listed')
|
|
115
|
+
|
|
116
|
+
const callResult = await client.callTool({
|
|
117
|
+
name: 'GetData',
|
|
118
|
+
arguments: {
|
|
119
|
+
keyword: 'Hello',
|
|
120
|
+
},
|
|
121
|
+
})
|
|
122
|
+
console.log(callResult.content)
|
|
123
|
+
console.log('sse client transport call tool done')
|
|
124
|
+
} catch (error) {
|
|
125
|
+
console.error('sse transport error:', error)
|
|
126
|
+
} finally {
|
|
127
|
+
await client.close()
|
|
128
|
+
console.log('sse client transport closed')
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
console.log('sse client transport done\n\n')
|
|
132
|
+
await sleep(1000)
|
|
133
|
+
}
|
|
134
|
+
{{/if}}
|
|
135
|
+
|
|
136
|
+
async function main() {
|
|
137
|
+
try {
|
|
138
|
+
{{#if (includes transports 'stdio')}}
|
|
139
|
+
await testStdioTransport()
|
|
140
|
+
{{/if}}
|
|
141
|
+
{{#if (includes transports 'streamable')}}
|
|
142
|
+
await testStreamableHttpTransport()
|
|
143
|
+
{{/if}}
|
|
144
|
+
{{#if (includes transports 'sse')}}
|
|
145
|
+
await testSSETransport()
|
|
146
|
+
{{/if}}
|
|
147
|
+
} catch (error) {
|
|
148
|
+
console.error('Main error:', error)
|
|
149
|
+
process.exit(1)
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
await main()
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
{{#if (includes plugins 'vitest')}}
|
|
2
|
+
import { describe, test } from 'vitest'
|
|
3
|
+
import { {{#each transports}} {{~#if (eq this "stdio")}}testStdioTransport{{/if}} {{~#if (eq this "streamable")}}testStreamableHttpTransport{{/if}} {{~#if (eq this "sse")}}testSSETransport{{/if}}{{~#unless @last}}, {{/unless}}{{/each}} } from '@/index.js'
|
|
4
|
+
|
|
5
|
+
describe('MCP client', () => {
|
|
6
|
+
{{#if (includes transports 'stdio')}}
|
|
7
|
+
test('stdio transport', async () => {
|
|
8
|
+
await testStdioTransport()
|
|
9
|
+
})
|
|
10
|
+
{{/if}}
|
|
11
|
+
{{#if (includes transports 'streamable')}}
|
|
12
|
+
test('streamable http transport', async () => {
|
|
13
|
+
await testStreamableHttpTransport()
|
|
14
|
+
})
|
|
15
|
+
{{/if}}
|
|
16
|
+
{{#if (includes transports 'sse')}}
|
|
17
|
+
test('sse transport', async () => {
|
|
18
|
+
await testSSETransport()
|
|
19
|
+
})
|
|
20
|
+
{{/if}}
|
|
21
|
+
})
|
|
22
|
+
{{/if}}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
{{#if (includes plugins 'vitest')}}
|
|
1
2
|
export function waitForValue(getterFn, checkInterval = 100, timeout = 10000) {
|
|
2
3
|
return new Promise((resolve, reject) => {
|
|
3
4
|
const start = Date.now()
|
|
@@ -14,3 +15,4 @@ export function waitForValue(getterFn, checkInterval = 100, timeout = 10000) {
|
|
|
14
15
|
}, checkInterval)
|
|
15
16
|
})
|
|
16
17
|
}
|
|
18
|
+
{{/if}}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
{{#if (includes plugins 'vitest')}}
|
|
2
|
+
import { defineConfig } from 'vitest/config'
|
|
3
|
+
|
|
4
|
+
export default defineConfig({
|
|
5
|
+
resolve: {
|
|
6
|
+
alias: {
|
|
7
|
+
'@': '/src',
|
|
8
|
+
},
|
|
9
|
+
},
|
|
10
|
+
test: {
|
|
11
|
+
{{#if (or (includes transports 'streamable') (includes transports 'sse'))}}
|
|
12
|
+
globalSetup: ['./vitest.global.js'],
|
|
13
|
+
{{/if}}
|
|
14
|
+
coverage: {
|
|
15
|
+
include: ['src/**/*.js'],
|
|
16
|
+
reporter: ['text', 'lcov', 'html'],
|
|
17
|
+
},
|
|
18
|
+
pool: 'threads',
|
|
19
|
+
poolOptions: {
|
|
20
|
+
threads: {
|
|
21
|
+
maxThreads: 1,
|
|
22
|
+
minThreads: 1,
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
})
|
|
27
|
+
{{/if}}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
{{#if (and (includes plugins 'vitest') (or (includes transports 'streamable') (includes transports 'sse')))}}
|
|
2
|
+
import { spawn } from 'child_process'
|
|
3
|
+
import { waitForValue } from './tests/utils'
|
|
4
|
+
|
|
5
|
+
export default async function setup() {
|
|
6
|
+
const webProcess = spawn('npx', ['-y', '@my-mcp-hub/node-mcp-server', 'web'], {
|
|
7
|
+
stdio: 'pipe',
|
|
8
|
+
})
|
|
9
|
+
let webStarted = false
|
|
10
|
+
webProcess.stdout.on('data', async (data) => {
|
|
11
|
+
const output = data.toString()
|
|
12
|
+
if (output.includes('MCP server started')) {
|
|
13
|
+
webStarted = true
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
await waitForValue(() => webStarted)
|
|
17
|
+
return () => {
|
|
18
|
+
webProcess.kill('SIGINT')
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
{{/if}}
|
|
File without changes
|