@yhuan416/mcp-kit 0.0.0-stage → 0.1.0

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.
Files changed (52) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/LICENSE +21 -0
  3. package/README.md +134 -2
  4. package/dist/env.cjs +114 -0
  5. package/dist/env.cjs.map +1 -0
  6. package/dist/env.d.cts +41 -0
  7. package/dist/env.d.ts +41 -0
  8. package/dist/env.js +105 -0
  9. package/dist/env.js.map +1 -0
  10. package/dist/http/index.cjs +567 -0
  11. package/dist/http/index.cjs.map +1 -0
  12. package/dist/http/index.d.cts +118 -0
  13. package/dist/http/index.d.ts +118 -0
  14. package/dist/http/index.js +555 -0
  15. package/dist/http/index.js.map +1 -0
  16. package/dist/index.cjs +460 -0
  17. package/dist/index.cjs.map +1 -0
  18. package/dist/index.d.cts +131 -0
  19. package/dist/index.d.ts +131 -0
  20. package/dist/index.js +432 -0
  21. package/dist/index.js.map +1 -0
  22. package/dist/info/index.cjs +376 -0
  23. package/dist/info/index.cjs.map +1 -0
  24. package/dist/info/index.d.cts +47 -0
  25. package/dist/info/index.d.ts +47 -0
  26. package/dist/info/index.js +363 -0
  27. package/dist/info/index.js.map +1 -0
  28. package/dist/testing/client.cjs +53 -0
  29. package/dist/testing/client.cjs.map +1 -0
  30. package/dist/testing/client.d.cts +33 -0
  31. package/dist/testing/client.d.ts +33 -0
  32. package/dist/testing/client.js +50 -0
  33. package/dist/testing/client.js.map +1 -0
  34. package/dist/testing/conformance.cjs +117 -0
  35. package/dist/testing/conformance.cjs.map +1 -0
  36. package/dist/testing/conformance.d.cts +26 -0
  37. package/dist/testing/conformance.d.ts +26 -0
  38. package/dist/testing/conformance.js +112 -0
  39. package/dist/testing/conformance.js.map +1 -0
  40. package/dist/types-D6AKhA-v.d.cts +95 -0
  41. package/dist/types-D6AKhA-v.d.ts +95 -0
  42. package/dist/types-DU3WHzhD.d.cts +76 -0
  43. package/dist/types-DU3WHzhD.d.ts +76 -0
  44. package/docs/contracts/env.md +77 -0
  45. package/docs/contracts/http.md +159 -0
  46. package/docs/contracts/info.md +133 -0
  47. package/docs/contracts/result.md +109 -0
  48. package/docs/contracts/tools.md +139 -0
  49. package/docs/migration.md +133 -0
  50. package/docs/release.md +106 -0
  51. package/docs/security.md +77 -0
  52. package/package.json +111 -4
@@ -0,0 +1,133 @@
1
+ # Migrating a vendored `src/sdk`
2
+
3
+ `serial-hub-mcp` and similar projects previously copied a `src/sdk` directory.
4
+ This package is the extracted, versioned version of that code. The migration is
5
+ mostly deletion and import replacement.
6
+
7
+ ## 1. Add the dependency
8
+
9
+ ```bash
10
+ npm install @yhuan416/mcp-kit
11
+ npm install -D @modelcontextprotocol/client
12
+ ```
13
+
14
+ Requirements:
15
+
16
+ - Node.js >= 22
17
+ - MCP SDK v2 (`@modelcontextprotocol/server`, `@modelcontextprotocol/node`)
18
+ - zod >= 4.2
19
+
20
+ Remove the old `@modelcontextprotocol/sdk` v1 dependency.
21
+
22
+ ## 2. Delete vendored helpers
23
+
24
+ Remove these from the service when they match the kit:
25
+
26
+ - `src/sdk/server.js`
27
+ - `src/sdk/tool.js`
28
+ - `src/sdk/result.js`
29
+ - `src/sdk/service-info.js`
30
+ - `src/sdk/info-page.js`
31
+ - `src/sdk/http.js`
32
+ - `src/keyed_queue.js`
33
+ - `src/secrets.js`
34
+ - `src/http_allowlist.js`
35
+ - `test-utils/mcp_client.js`
36
+
37
+ Keep service-specific code: info data builders, stores, routers, tools, web API.
38
+
39
+ ## 3. Replace imports
40
+
41
+ | Before | After |
42
+ | --- | --- |
43
+ | `./sdk/server.js` | `@yhuan416/mcp-kit` |
44
+ | `./sdk/tool.js` | `@yhuan416/mcp-kit` |
45
+ | `./sdk/result.js` | `@yhuan416/mcp-kit` |
46
+ | `./sdk/service-info.js` | `@yhuan416/mcp-kit/info` |
47
+ | `./sdk/info-page.js` | `@yhuan416/mcp-kit/info` |
48
+ | `./sdk/http.js` | `@yhuan416/mcp-kit/http` |
49
+ | `./keyed_queue.js` | `@yhuan416/mcp-kit` (`keyedSerialQueue`) |
50
+ | `./secrets.js` | `@yhuan416/mcp-kit/env` (`readSecret`) |
51
+ | `./http_allowlist.js` | `@yhuan416/mcp-kit/env` (`buildHttpAllowlist`) |
52
+ | `test-utils/mcp_client.js` | `@yhuan416/mcp-kit/testing` |
53
+
54
+ ## 4. Entry points
55
+
56
+ stdio:
57
+
58
+ ```ts
59
+ // before: runStdio(server, { name })
60
+ runStdio(() => buildServer({ name, version, tools, queue }), { name });
61
+ ```
62
+
63
+ The factory may be called more than once (connection plus discovery probe), so
64
+ return a fresh server each time.
65
+
66
+ HTTP:
67
+
68
+ ```ts
69
+ // before: runHttp({ createServer, ... })
70
+ await runHttp({
71
+ createServer: () => buildServer({ name, version, tools, queue }),
72
+ auth: { token: process.env.SERVICE_HTTP_TOKEN! },
73
+ info,
74
+ files: { resolve: resolveDump },
75
+ webDir: UI_DIR,
76
+ apiHandler: createWebApi(...),
77
+ });
78
+ ```
79
+
80
+ `runHttp` returns `{ server, port, close }`.
81
+
82
+ ## 5. Tool handlers
83
+
84
+ Add the optional context when you need auth, tracing, cancellation, or tenant
85
+ identity:
86
+
87
+ ```ts
88
+ handler: async (args, ctx) => {
89
+ const key = `${ctx.principal ?? "anon"}:${args.connection_id}`;
90
+ return doWork(args, key);
91
+ }
92
+ ```
93
+
94
+ Existing `(args) => ...` handlers keep working.
95
+
96
+ ## 6. Queue keys
97
+
98
+ If the service is multi-user, include the principal:
99
+
100
+ ```ts
101
+ buildServer({
102
+ ...,
103
+ queue: keyedSerialQueue(),
104
+ key: (args, ctx) => `${ctx.principal ?? "anon"}:${args.connection_id}`,
105
+ });
106
+ ```
107
+
108
+ ## 7. Info
109
+
110
+ Keep the service's data builder (`src/info.js`), but use the kit's builders:
111
+
112
+ ```ts
113
+ import { createInfoEndpoint, dirStats, formatBytes, resolvePublicUrl } from "@yhuan416/mcp-kit/info";
114
+ ```
115
+
116
+ Add `contract: "info/1"` by using `buildServiceInfo`/`createInfoEndpoint`.
117
+
118
+ ## 8. Verify
119
+
120
+ 1. `npm run typecheck`
121
+ 2. `npm run lint`
122
+ 3. `npm test`
123
+ 4. Run the conformance checks:
124
+
125
+ ```ts
126
+ import { checkToolDefinitions, checkServiceInfo, assertConformant } from "@yhuan416/mcp-kit/conformance";
127
+
128
+ assertConformant(checkToolDefinitions(tools), { allowWarnings: true });
129
+ assertConformant(checkServiceInfo(info.build()));
130
+ ```
131
+
132
+ 5. Smoke-test both stdio and HTTP, including an unauthenticated `/health` and
133
+ `/info`, and a 401 on `/mcp`.
@@ -0,0 +1,106 @@
1
+ # Release and repository settings
2
+
3
+ This document covers the GitHub-side workflow and the settings that cannot live
4
+ in the repository.
5
+
6
+ ## Workflows
7
+
8
+ | Workflow | Trigger | Purpose |
9
+ | ---------- | ------------------------------ | ---------------------------------------------------------------- |
10
+ | `CI` | push to `master`, pull request | Node 22/24: typecheck, lint, test, build, pack, production audit |
11
+ | `CodeQL` | push to `master`, PR, weekly | JavaScript/TypeScript security analysis |
12
+ | `Release` | tag `vX.Y.Z`, manual | Verify, build, and publish to npm |
13
+ | Dependabot | weekly | npm and GitHub Actions updates |
14
+
15
+ All workflows use least-privilege `permissions` and CI cancels superseded runs.
16
+
17
+ ## Trusted publishing
18
+
19
+ The release workflow is set up for npm trusted publishing (OIDC). It uses
20
+ `id-token: write` and npm >= 11.5.1; Node 24 on the runner provides it.
21
+ `NPM_TOKEN` is only a fallback for the very first publish before the trusted
22
+ publisher exists.
23
+
24
+ Configure the trusted publisher once, with 2FA:
25
+
26
+ ```bash
27
+ npm trust github @yhuan416/mcp-kit \
28
+ --file release.yml \
29
+ --repo yhuan416/mcp-kit \
30
+ --env npm \
31
+ --allow-publish -y
32
+ ```
33
+
34
+ This binds the package to:
35
+
36
+ - repository `yhuan416/mcp-kit`
37
+ - workflow `.github/workflows/release.yml`
38
+ - environment `npm`
39
+
40
+ If npm refuses because the package does not exist yet, publish `0.1.0` once with
41
+ a granular/automation token, then run the command above for all later releases.
42
+
43
+ Do not keep an `NPM_TOKEN` secret after OIDC publishing works.
44
+
45
+ ## Release
46
+
47
+ 1. Bump `package.json`, `package-lock.json`, and `CHANGELOG.md`.
48
+ 2. Merge to `master` through a PR.
49
+ 3. Tag and push:
50
+
51
+ ```bash
52
+ git tag v0.1.0
53
+ git push origin v0.1.0
54
+ ```
55
+
56
+ The `Release` workflow checks that the tag equals `package.json.version`, runs
57
+ typecheck/lint/test/build/pack, skips an already-published version, publishes
58
+ with provenance, and creates a GitHub Release.
59
+
60
+ Run a dry-run first:
61
+
62
+ ```bash
63
+ gh workflow run release.yml --field dry_run=true
64
+ ```
65
+
66
+ ## Branch protection / rulesets
67
+
68
+ Configure a ruleset for `master`:
69
+
70
+ - Require a pull request before merging (0 approvals is fine for a solo maintainer).
71
+ - Require status checks:
72
+ - `CI / test (22)`
73
+ - `CI / test (24)`
74
+ - `CodeQL / Analyze (javascript-typescript)`
75
+ - Require branches to be up to date before merging.
76
+ - Require conversation resolution.
77
+ - Block force pushes.
78
+ - Block deletions.
79
+ - Do not allow bypassing the ruleset.
80
+
81
+ Configure a tag ruleset for `v*`:
82
+
83
+ - Restrict creation/update/deletion to maintainers.
84
+ - This prevents an accidental tag from triggering a publish.
85
+
86
+ ## Repository settings
87
+
88
+ - **Actions → General**: allow GitHub Actions; default `GITHUB_TOKEN`
89
+ permissions = read-only.
90
+ - **Code security**: enable Dependabot alerts, Dependabot security updates,
91
+ secret scanning, and push protection.
92
+ - **Branches**: enable automatic deletion of merged head branches.
93
+ - **Environments**: create `npm` with required reviewers if publishing should
94
+ be manual.
95
+
96
+ ## Checklist before the first release
97
+
98
+ - [ ] CI green on `master` (Node 22 and 24).
99
+ - [ ] CodeQL green.
100
+ - [ ] Trusted publisher configured (`npm trust github ...`), or `NPM_TOKEN` fallback set for the first publish.
101
+ - [ ] `npm` environment created (optional gate).
102
+ - [ ] `master` ruleset requires the checks above.
103
+ - [ ] `v*` tag ruleset restricts creation.
104
+ - [ ] Secret scanning and push protection enabled.
105
+ - [ ] `package.json` version matches the tag.
106
+ - [ ] `CHANGELOG.md` updated.
@@ -0,0 +1,77 @@
1
+ # Security baseline
2
+
3
+ This document describes the defaults the kit provides and the responsibilities it
4
+ leaves to the service.
5
+
6
+ ## Defaults
7
+
8
+ - HTTP binds `127.0.0.1` unless configured otherwise.
9
+ - Non-loopback binds require auth unless `allowAnonymous: true` is explicit.
10
+ - Tokens are compared in constant time.
11
+ - `/health`, `/ready`, and `/info` are unauthenticated and must not contain
12
+ secrets.
13
+ - The default body limit is 4 MiB.
14
+ - File downloads use an opaque-id resolver, not a URL path.
15
+ - Static paths are resolved inside a root and reject traversal.
16
+ - UI assets are served from an explicit vendor whitelist; no directory listing.
17
+ - Tool registration order is deterministic.
18
+
19
+ ## Secrets
20
+
21
+ - Read secrets from `NAME` or `NAME_FILE` (`readSecret`).
22
+ - Never accept secrets as tool arguments.
23
+ - Never log secrets; use `maskSecret`.
24
+ - Never include secrets in `/info`, error messages, or tool responses.
25
+ - Prefer Kubernetes/Docker secret files over environment variables.
26
+ - Rotate tokens by restarting with a new `_FILE` value.
27
+
28
+ ## Authentication and authorization
29
+
30
+ The kit provides Bearer token verification. If you need OAuth, per-tool scopes,
31
+ or multi-tenant policy, implement it in the `auth` callback or in the tool
32
+ handler using `ctx.auth` / `ctx.principal`.
33
+
34
+ - Do not use the same token for multiple tenants unless they are intended to
35
+ share data.
36
+ - Include the principal in queue keys and any per-tenant storage key.
37
+ - Never trust `principal` from request headers; derive it from validated auth.
38
+
39
+ ## HTTP
40
+
41
+ - Keep the default Host/Origin allowlist for loopback binds.
42
+ - Set explicit allowlists when binding to a network.
43
+ - Put `/api/*` behind the same auth as `/mcp`.
44
+ - Do not expose `/files` without auth unless the files are public.
45
+ - Set request body limits appropriate for the service.
46
+ - Terminate TLS at a trusted reverse proxy and forward
47
+ `X-Forwarded-Proto`/`Host` correctly.
48
+
49
+ ## Tool results
50
+
51
+ - Business errors are results, not JSON-RPC errors.
52
+ - Error messages should be actionable but must not leak secrets or file
53
+ contents.
54
+ - Validate all inputs with a Standard Schema.
55
+ - Treat `details` as public; it is returned to the client.
56
+
57
+ ## Async features
58
+
59
+ Tasks, Events, and webhooks are intentionally out of scope for `0.1.x`.
60
+ If you add them in the service:
61
+
62
+ - task ids must be unguessable and scoped to the principal;
63
+ - webhook payloads must be signed and verified;
64
+ - webhook handlers must be idempotent;
65
+ - subscriptions must be authorized per principal.
66
+
67
+ ## Dependencies
68
+
69
+ - Node.js >= 22.
70
+ - MCP SDK v2 and zod 4 are peers; pin compatible versions.
71
+ - Run `npm audit` and keep the lockfile committed.
72
+ - Publish with npm provenance when the repository is public.
73
+
74
+ ## Reporting
75
+
76
+ Please report security issues privately to the maintainer listed in
77
+ `package.json`, not in a public issue.
package/package.json CHANGED
@@ -1,6 +1,113 @@
1
1
  {
2
2
  "name": "@yhuan416/mcp-kit",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Operational and interface conventions for production MCP servers: tool contracts, result envelope, health/info endpoints, auth, files, and conformance checks.",
5
+ "type": "module",
6
+ "license": "MIT",
7
+ "author": "yhuan416 <yhuan416@foxmail.com>",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "git+https://github.com/yhuan416/mcp-kit.git"
11
+ },
12
+ "bugs": {
13
+ "url": "https://github.com/yhuan416/mcp-kit/issues"
14
+ },
15
+ "homepage": "https://github.com/yhuan416/mcp-kit#readme",
16
+ "keywords": [
17
+ "mcp",
18
+ "model-context-protocol",
19
+ "typescript",
20
+ "server",
21
+ "ops",
22
+ "health",
23
+ "info",
24
+ "bearer-auth"
25
+ ],
26
+ "engines": {
27
+ "node": ">=22"
28
+ },
29
+ "main": "./dist/index.cjs",
30
+ "module": "./dist/index.js",
31
+ "types": "./dist/index.d.ts",
32
+ "exports": {
33
+ ".": {
34
+ "types": "./dist/index.d.ts",
35
+ "import": "./dist/index.js",
36
+ "require": "./dist/index.cjs"
37
+ },
38
+ "./http": {
39
+ "types": "./dist/http/index.d.ts",
40
+ "import": "./dist/http/index.js",
41
+ "require": "./dist/http/index.cjs"
42
+ },
43
+ "./info": {
44
+ "types": "./dist/info/index.d.ts",
45
+ "import": "./dist/info/index.js",
46
+ "require": "./dist/info/index.cjs"
47
+ },
48
+ "./env": {
49
+ "types": "./dist/env.d.ts",
50
+ "import": "./dist/env.js",
51
+ "require": "./dist/env.cjs"
52
+ },
53
+ "./testing": {
54
+ "types": "./dist/testing/client.d.ts",
55
+ "import": "./dist/testing/client.js",
56
+ "require": "./dist/testing/client.cjs"
57
+ },
58
+ "./conformance": {
59
+ "types": "./dist/testing/conformance.d.ts",
60
+ "import": "./dist/testing/conformance.js",
61
+ "require": "./dist/testing/conformance.cjs"
62
+ }
63
+ },
64
+ "files": [
65
+ "dist",
66
+ "docs",
67
+ "CHANGELOG.md",
68
+ "README.md",
69
+ "LICENSE"
70
+ ],
71
+ "sideEffects": false,
72
+ "publishConfig": {
73
+ "access": "public"
74
+ },
75
+ "scripts": {
76
+ "build": "tsup",
77
+ "dev": "tsup --watch",
78
+ "typecheck": "tsc --noEmit",
79
+ "test": "vitest run",
80
+ "test:watch": "vitest",
81
+ "lint": "eslint . && prettier --check \"src/**/*.ts\" \"test/**/*.ts\" \"examples/**/*.ts\"",
82
+ "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\" \"examples/**/*.ts\"",
83
+ "prepublishOnly": "npm run typecheck && npm run lint && npm test && npm run build"
84
+ },
85
+ "peerDependencies": {
86
+ "@modelcontextprotocol/client": "^2.4.0",
87
+ "@modelcontextprotocol/node": "^2.1.2",
88
+ "@modelcontextprotocol/server": "^2.4.0",
89
+ "zod": "^4.2.0"
90
+ },
91
+ "peerDependenciesMeta": {
92
+ "@modelcontextprotocol/client": {
93
+ "optional": true
94
+ },
95
+ "@modelcontextprotocol/node": {
96
+ "optional": true
97
+ }
98
+ },
99
+ "devDependencies": {
100
+ "@eslint/js": "^9.39.5",
101
+ "@modelcontextprotocol/client": "^2.4.0",
102
+ "@modelcontextprotocol/node": "^2.1.2",
103
+ "@modelcontextprotocol/server": "^2.4.0",
104
+ "@types/node": "^22.10.0",
105
+ "eslint": "^9.39.5",
106
+ "prettier": "^3.9.10",
107
+ "tsup": "^9.0.0",
108
+ "typescript": "^5.9.0",
109
+ "typescript-eslint": "^8.48.0",
110
+ "vitest": "^4.0.0",
111
+ "zod": "^4.6.7"
112
+ }
113
+ }