gitx.do 0.1.1 → 0.1.3
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 +40 -353
- package/dist/do/logger.d.ts +50 -0
- package/dist/do/logger.d.ts.map +1 -0
- package/dist/do/logger.js +122 -0
- package/dist/do/logger.js.map +1 -0
- package/dist/{durable-object → do}/schema.d.ts +3 -3
- package/dist/do/schema.d.ts.map +1 -0
- package/dist/{durable-object → do}/schema.js +4 -3
- package/dist/do/schema.js.map +1 -0
- package/dist/do/types.d.ts +267 -0
- package/dist/do/types.d.ts.map +1 -0
- package/dist/do/types.js +62 -0
- package/dist/do/types.js.map +1 -0
- package/dist/index.d.ts +15 -469
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +39 -481
- package/dist/index.js.map +1 -1
- package/dist/mcp/auth.d.ts +77 -0
- package/dist/mcp/auth.d.ts.map +1 -0
- package/dist/mcp/auth.js +278 -0
- package/dist/mcp/auth.js.map +1 -0
- package/dist/mcp/index.d.ts +13 -0
- package/dist/mcp/index.d.ts.map +1 -0
- package/dist/mcp/index.js +19 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/mcp/server.d.ts +200 -0
- package/dist/mcp/server.d.ts.map +1 -0
- package/dist/mcp/server.js +275 -0
- package/dist/mcp/server.js.map +1 -0
- package/dist/mcp/tool-registry.d.ts +47 -0
- package/dist/mcp/tool-registry.d.ts.map +1 -0
- package/dist/mcp/tool-registry.js +284 -0
- package/dist/mcp/tool-registry.js.map +1 -0
- package/dist/mcp/tools.d.ts +103 -515
- package/dist/mcp/tools.d.ts.map +1 -1
- package/dist/mcp/tools.js +676 -3087
- package/dist/mcp/tools.js.map +1 -1
- package/dist/mcp/types.d.ts +124 -0
- package/dist/mcp/types.d.ts.map +1 -0
- package/dist/mcp/types.js +9 -0
- package/dist/mcp/types.js.map +1 -0
- package/package.json +19 -21
- package/dist/cli/commands/add.d.ts +0 -176
- package/dist/cli/commands/add.d.ts.map +0 -1
- package/dist/cli/commands/add.js +0 -979
- package/dist/cli/commands/add.js.map +0 -1
- package/dist/cli/commands/blame.d.ts +0 -259
- package/dist/cli/commands/blame.d.ts.map +0 -1
- package/dist/cli/commands/blame.js +0 -609
- package/dist/cli/commands/blame.js.map +0 -1
- package/dist/cli/commands/branch.d.ts +0 -249
- package/dist/cli/commands/branch.d.ts.map +0 -1
- package/dist/cli/commands/branch.js +0 -693
- package/dist/cli/commands/branch.js.map +0 -1
- package/dist/cli/commands/checkout.d.ts +0 -73
- package/dist/cli/commands/checkout.d.ts.map +0 -1
- package/dist/cli/commands/checkout.js +0 -725
- package/dist/cli/commands/checkout.js.map +0 -1
- package/dist/cli/commands/commit.d.ts +0 -182
- package/dist/cli/commands/commit.d.ts.map +0 -1
- package/dist/cli/commands/commit.js +0 -457
- package/dist/cli/commands/commit.js.map +0 -1
- package/dist/cli/commands/diff.d.ts +0 -464
- package/dist/cli/commands/diff.d.ts.map +0 -1
- package/dist/cli/commands/diff.js +0 -959
- package/dist/cli/commands/diff.js.map +0 -1
- package/dist/cli/commands/log.d.ts +0 -239
- package/dist/cli/commands/log.d.ts.map +0 -1
- package/dist/cli/commands/log.js +0 -535
- package/dist/cli/commands/log.js.map +0 -1
- package/dist/cli/commands/merge.d.ts +0 -106
- package/dist/cli/commands/merge.d.ts.map +0 -1
- package/dist/cli/commands/merge.js +0 -852
- package/dist/cli/commands/merge.js.map +0 -1
- package/dist/cli/commands/review.d.ts +0 -457
- package/dist/cli/commands/review.d.ts.map +0 -1
- package/dist/cli/commands/review.js +0 -558
- package/dist/cli/commands/review.js.map +0 -1
- package/dist/cli/commands/stash.d.ts +0 -157
- package/dist/cli/commands/stash.d.ts.map +0 -1
- package/dist/cli/commands/stash.js +0 -655
- package/dist/cli/commands/stash.js.map +0 -1
- package/dist/cli/commands/status.d.ts +0 -269
- package/dist/cli/commands/status.d.ts.map +0 -1
- package/dist/cli/commands/status.js +0 -492
- package/dist/cli/commands/status.js.map +0 -1
- package/dist/cli/commands/web.d.ts +0 -199
- package/dist/cli/commands/web.d.ts.map +0 -1
- package/dist/cli/commands/web.js +0 -697
- package/dist/cli/commands/web.js.map +0 -1
- package/dist/cli/fs-adapter.d.ts +0 -656
- package/dist/cli/fs-adapter.d.ts.map +0 -1
- package/dist/cli/fs-adapter.js +0 -1177
- package/dist/cli/fs-adapter.js.map +0 -1
- package/dist/cli/fsx-cli-adapter.d.ts +0 -359
- package/dist/cli/fsx-cli-adapter.d.ts.map +0 -1
- package/dist/cli/fsx-cli-adapter.js +0 -619
- package/dist/cli/fsx-cli-adapter.js.map +0 -1
- package/dist/cli/index.d.ts +0 -387
- package/dist/cli/index.d.ts.map +0 -1
- package/dist/cli/index.js +0 -579
- package/dist/cli/index.js.map +0 -1
- package/dist/cli/ui/components/DiffView.d.ts +0 -12
- package/dist/cli/ui/components/DiffView.d.ts.map +0 -1
- package/dist/cli/ui/components/DiffView.js +0 -11
- package/dist/cli/ui/components/DiffView.js.map +0 -1
- package/dist/cli/ui/components/ErrorDisplay.d.ts +0 -10
- package/dist/cli/ui/components/ErrorDisplay.d.ts.map +0 -1
- package/dist/cli/ui/components/ErrorDisplay.js +0 -11
- package/dist/cli/ui/components/ErrorDisplay.js.map +0 -1
- package/dist/cli/ui/components/FuzzySearch.d.ts +0 -15
- package/dist/cli/ui/components/FuzzySearch.d.ts.map +0 -1
- package/dist/cli/ui/components/FuzzySearch.js +0 -12
- package/dist/cli/ui/components/FuzzySearch.js.map +0 -1
- package/dist/cli/ui/components/LoadingSpinner.d.ts +0 -10
- package/dist/cli/ui/components/LoadingSpinner.d.ts.map +0 -1
- package/dist/cli/ui/components/LoadingSpinner.js +0 -10
- package/dist/cli/ui/components/LoadingSpinner.js.map +0 -1
- package/dist/cli/ui/components/NavigationList.d.ts +0 -14
- package/dist/cli/ui/components/NavigationList.d.ts.map +0 -1
- package/dist/cli/ui/components/NavigationList.js +0 -11
- package/dist/cli/ui/components/NavigationList.js.map +0 -1
- package/dist/cli/ui/components/ScrollableContent.d.ts +0 -13
- package/dist/cli/ui/components/ScrollableContent.d.ts.map +0 -1
- package/dist/cli/ui/components/ScrollableContent.js +0 -11
- package/dist/cli/ui/components/ScrollableContent.js.map +0 -1
- package/dist/cli/ui/components/index.d.ts +0 -7
- package/dist/cli/ui/components/index.d.ts.map +0 -1
- package/dist/cli/ui/components/index.js +0 -9
- package/dist/cli/ui/components/index.js.map +0 -1
- package/dist/cli/ui/terminal-ui.d.ts +0 -85
- package/dist/cli/ui/terminal-ui.d.ts.map +0 -1
- package/dist/cli/ui/terminal-ui.js +0 -121
- package/dist/cli/ui/terminal-ui.js.map +0 -1
- package/dist/do/BashModule.d.ts +0 -871
- package/dist/do/BashModule.d.ts.map +0 -1
- package/dist/do/BashModule.js +0 -1143
- package/dist/do/BashModule.js.map +0 -1
- package/dist/do/FsModule.d.ts +0 -612
- package/dist/do/FsModule.d.ts.map +0 -1
- package/dist/do/FsModule.js +0 -1120
- package/dist/do/FsModule.js.map +0 -1
- package/dist/do/GitModule.d.ts +0 -635
- package/dist/do/GitModule.d.ts.map +0 -1
- package/dist/do/GitModule.js +0 -784
- package/dist/do/GitModule.js.map +0 -1
- package/dist/do/GitRepoDO.d.ts +0 -281
- package/dist/do/GitRepoDO.d.ts.map +0 -1
- package/dist/do/GitRepoDO.js +0 -479
- package/dist/do/GitRepoDO.js.map +0 -1
- package/dist/do/bash-ast.d.ts +0 -246
- package/dist/do/bash-ast.d.ts.map +0 -1
- package/dist/do/bash-ast.js +0 -888
- package/dist/do/bash-ast.js.map +0 -1
- package/dist/do/container-executor.d.ts +0 -491
- package/dist/do/container-executor.d.ts.map +0 -1
- package/dist/do/container-executor.js +0 -731
- package/dist/do/container-executor.js.map +0 -1
- package/dist/do/index.d.ts +0 -53
- package/dist/do/index.d.ts.map +0 -1
- package/dist/do/index.js +0 -91
- package/dist/do/index.js.map +0 -1
- package/dist/do/tiered-storage.d.ts +0 -403
- package/dist/do/tiered-storage.d.ts.map +0 -1
- package/dist/do/tiered-storage.js +0 -689
- package/dist/do/tiered-storage.js.map +0 -1
- package/dist/do/withBash.d.ts +0 -231
- package/dist/do/withBash.d.ts.map +0 -1
- package/dist/do/withBash.js +0 -244
- package/dist/do/withBash.js.map +0 -1
- package/dist/do/withFs.d.ts +0 -237
- package/dist/do/withFs.d.ts.map +0 -1
- package/dist/do/withFs.js +0 -387
- package/dist/do/withFs.js.map +0 -1
- package/dist/do/withGit.d.ts +0 -180
- package/dist/do/withGit.d.ts.map +0 -1
- package/dist/do/withGit.js +0 -271
- package/dist/do/withGit.js.map +0 -1
- package/dist/durable-object/object-store.d.ts +0 -633
- package/dist/durable-object/object-store.d.ts.map +0 -1
- package/dist/durable-object/object-store.js +0 -1164
- package/dist/durable-object/object-store.js.map +0 -1
- package/dist/durable-object/schema.d.ts.map +0 -1
- package/dist/durable-object/schema.js.map +0 -1
- package/dist/durable-object/wal.d.ts +0 -416
- package/dist/durable-object/wal.d.ts.map +0 -1
- package/dist/durable-object/wal.js +0 -445
- package/dist/durable-object/wal.js.map +0 -1
- package/dist/mcp/adapter.d.ts +0 -772
- package/dist/mcp/adapter.d.ts.map +0 -1
- package/dist/mcp/adapter.js +0 -895
- package/dist/mcp/adapter.js.map +0 -1
- package/dist/mcp/sandbox/miniflare-evaluator.d.ts +0 -22
- package/dist/mcp/sandbox/miniflare-evaluator.d.ts.map +0 -1
- package/dist/mcp/sandbox/miniflare-evaluator.js +0 -140
- package/dist/mcp/sandbox/miniflare-evaluator.js.map +0 -1
- package/dist/mcp/sandbox/object-store-proxy.d.ts +0 -32
- package/dist/mcp/sandbox/object-store-proxy.d.ts.map +0 -1
- package/dist/mcp/sandbox/object-store-proxy.js +0 -30
- package/dist/mcp/sandbox/object-store-proxy.js.map +0 -1
- package/dist/mcp/sandbox/template.d.ts +0 -17
- package/dist/mcp/sandbox/template.d.ts.map +0 -1
- package/dist/mcp/sandbox/template.js +0 -71
- package/dist/mcp/sandbox/template.js.map +0 -1
- package/dist/mcp/sandbox.d.ts +0 -764
- package/dist/mcp/sandbox.d.ts.map +0 -1
- package/dist/mcp/sandbox.js +0 -1362
- package/dist/mcp/sandbox.js.map +0 -1
- package/dist/mcp/sdk-adapter.d.ts +0 -835
- package/dist/mcp/sdk-adapter.d.ts.map +0 -1
- package/dist/mcp/sdk-adapter.js +0 -974
- package/dist/mcp/sdk-adapter.js.map +0 -1
- package/dist/mcp/tools/do.d.ts +0 -32
- package/dist/mcp/tools/do.d.ts.map +0 -1
- package/dist/mcp/tools/do.js +0 -117
- package/dist/mcp/tools/do.js.map +0 -1
- package/dist/ops/blame.d.ts +0 -551
- package/dist/ops/blame.d.ts.map +0 -1
- package/dist/ops/blame.js +0 -1037
- package/dist/ops/blame.js.map +0 -1
- package/dist/ops/branch.d.ts +0 -766
- package/dist/ops/branch.d.ts.map +0 -1
- package/dist/ops/branch.js +0 -950
- package/dist/ops/branch.js.map +0 -1
- package/dist/ops/commit-traversal.d.ts +0 -349
- package/dist/ops/commit-traversal.d.ts.map +0 -1
- package/dist/ops/commit-traversal.js +0 -821
- package/dist/ops/commit-traversal.js.map +0 -1
- package/dist/ops/commit.d.ts +0 -555
- package/dist/ops/commit.d.ts.map +0 -1
- package/dist/ops/commit.js +0 -826
- package/dist/ops/commit.js.map +0 -1
- package/dist/ops/merge-base.d.ts +0 -397
- package/dist/ops/merge-base.d.ts.map +0 -1
- package/dist/ops/merge-base.js +0 -691
- package/dist/ops/merge-base.js.map +0 -1
- package/dist/ops/merge.d.ts +0 -855
- package/dist/ops/merge.d.ts.map +0 -1
- package/dist/ops/merge.js +0 -1551
- package/dist/ops/merge.js.map +0 -1
- package/dist/ops/tag.d.ts +0 -247
- package/dist/ops/tag.d.ts.map +0 -1
- package/dist/ops/tag.js +0 -649
- package/dist/ops/tag.js.map +0 -1
- package/dist/ops/tree-builder.d.ts +0 -178
- package/dist/ops/tree-builder.d.ts.map +0 -1
- package/dist/ops/tree-builder.js +0 -271
- package/dist/ops/tree-builder.js.map +0 -1
- package/dist/ops/tree-diff.d.ts +0 -291
- package/dist/ops/tree-diff.d.ts.map +0 -1
- package/dist/ops/tree-diff.js +0 -705
- package/dist/ops/tree-diff.js.map +0 -1
- package/dist/pack/delta.d.ts +0 -248
- package/dist/pack/delta.d.ts.map +0 -1
- package/dist/pack/delta.js +0 -740
- package/dist/pack/delta.js.map +0 -1
- package/dist/pack/format.d.ts +0 -446
- package/dist/pack/format.d.ts.map +0 -1
- package/dist/pack/format.js +0 -572
- package/dist/pack/format.js.map +0 -1
- package/dist/pack/full-generation.d.ts +0 -612
- package/dist/pack/full-generation.d.ts.map +0 -1
- package/dist/pack/full-generation.js +0 -1378
- package/dist/pack/full-generation.js.map +0 -1
- package/dist/pack/generation.d.ts +0 -441
- package/dist/pack/generation.d.ts.map +0 -1
- package/dist/pack/generation.js +0 -707
- package/dist/pack/generation.js.map +0 -1
- package/dist/pack/index.d.ts +0 -502
- package/dist/pack/index.d.ts.map +0 -1
- package/dist/pack/index.js +0 -833
- package/dist/pack/index.js.map +0 -1
- package/dist/refs/branch.d.ts +0 -683
- package/dist/refs/branch.d.ts.map +0 -1
- package/dist/refs/branch.js +0 -881
- package/dist/refs/branch.js.map +0 -1
- package/dist/refs/storage.d.ts +0 -833
- package/dist/refs/storage.d.ts.map +0 -1
- package/dist/refs/storage.js +0 -1023
- package/dist/refs/storage.js.map +0 -1
- package/dist/refs/tag.d.ts +0 -860
- package/dist/refs/tag.d.ts.map +0 -1
- package/dist/refs/tag.js +0 -996
- package/dist/refs/tag.js.map +0 -1
- package/dist/storage/backend.d.ts +0 -425
- package/dist/storage/backend.d.ts.map +0 -1
- package/dist/storage/backend.js +0 -41
- package/dist/storage/backend.js.map +0 -1
- package/dist/storage/fsx-adapter.d.ts +0 -204
- package/dist/storage/fsx-adapter.d.ts.map +0 -1
- package/dist/storage/fsx-adapter.js +0 -518
- package/dist/storage/fsx-adapter.js.map +0 -1
- package/dist/storage/lru-cache.d.ts +0 -691
- package/dist/storage/lru-cache.d.ts.map +0 -1
- package/dist/storage/lru-cache.js +0 -813
- package/dist/storage/lru-cache.js.map +0 -1
- package/dist/storage/object-index.d.ts +0 -585
- package/dist/storage/object-index.d.ts.map +0 -1
- package/dist/storage/object-index.js +0 -532
- package/dist/storage/object-index.js.map +0 -1
- package/dist/storage/r2-pack.d.ts +0 -1257
- package/dist/storage/r2-pack.d.ts.map +0 -1
- package/dist/storage/r2-pack.js +0 -1773
- package/dist/storage/r2-pack.js.map +0 -1
- package/dist/tiered/cdc-pipeline.d.ts +0 -1888
- package/dist/tiered/cdc-pipeline.d.ts.map +0 -1
- package/dist/tiered/cdc-pipeline.js +0 -1880
- package/dist/tiered/cdc-pipeline.js.map +0 -1
- package/dist/tiered/migration.d.ts +0 -1104
- package/dist/tiered/migration.d.ts.map +0 -1
- package/dist/tiered/migration.js +0 -1217
- package/dist/tiered/migration.js.map +0 -1
- package/dist/tiered/parquet-writer.d.ts +0 -1145
- package/dist/tiered/parquet-writer.d.ts.map +0 -1
- package/dist/tiered/parquet-writer.js +0 -1183
- package/dist/tiered/parquet-writer.js.map +0 -1
- package/dist/tiered/read-path.d.ts +0 -835
- package/dist/tiered/read-path.d.ts.map +0 -1
- package/dist/tiered/read-path.js +0 -487
- package/dist/tiered/read-path.js.map +0 -1
- package/dist/types/capability.d.ts +0 -1385
- package/dist/types/capability.d.ts.map +0 -1
- package/dist/types/capability.js +0 -36
- package/dist/types/capability.js.map +0 -1
- package/dist/types/index.d.ts +0 -13
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/index.js +0 -18
- package/dist/types/index.js.map +0 -1
- package/dist/types/interfaces.d.ts +0 -673
- package/dist/types/interfaces.d.ts.map +0 -1
- package/dist/types/interfaces.js +0 -26
- package/dist/types/interfaces.js.map +0 -1
- package/dist/types/objects.d.ts +0 -692
- package/dist/types/objects.d.ts.map +0 -1
- package/dist/types/objects.js +0 -837
- package/dist/types/objects.js.map +0 -1
- package/dist/types/storage.d.ts +0 -603
- package/dist/types/storage.d.ts.map +0 -1
- package/dist/types/storage.js +0 -191
- package/dist/types/storage.js.map +0 -1
- package/dist/types/worker-loader.d.ts +0 -60
- package/dist/types/worker-loader.d.ts.map +0 -1
- package/dist/types/worker-loader.js +0 -62
- package/dist/types/worker-loader.js.map +0 -1
- package/dist/utils/hash.d.ts +0 -198
- package/dist/utils/hash.d.ts.map +0 -1
- package/dist/utils/hash.js +0 -272
- package/dist/utils/hash.js.map +0 -1
- package/dist/utils/sha1.d.ts +0 -325
- package/dist/utils/sha1.d.ts.map +0 -1
- package/dist/utils/sha1.js +0 -635
- package/dist/utils/sha1.js.map +0 -1
- package/dist/wire/capabilities.d.ts +0 -1044
- package/dist/wire/capabilities.d.ts.map +0 -1
- package/dist/wire/capabilities.js +0 -941
- package/dist/wire/capabilities.js.map +0 -1
- package/dist/wire/path-security.d.ts +0 -157
- package/dist/wire/path-security.d.ts.map +0 -1
- package/dist/wire/path-security.js +0 -307
- package/dist/wire/path-security.js.map +0 -1
- package/dist/wire/pkt-line.d.ts +0 -345
- package/dist/wire/pkt-line.d.ts.map +0 -1
- package/dist/wire/pkt-line.js +0 -381
- package/dist/wire/pkt-line.js.map +0 -1
- package/dist/wire/receive-pack.d.ts +0 -1059
- package/dist/wire/receive-pack.d.ts.map +0 -1
- package/dist/wire/receive-pack.js +0 -1414
- package/dist/wire/receive-pack.js.map +0 -1
- package/dist/wire/smart-http.d.ts +0 -799
- package/dist/wire/smart-http.d.ts.map +0 -1
- package/dist/wire/smart-http.js +0 -945
- package/dist/wire/smart-http.js.map +0 -1
- package/dist/wire/upload-pack.d.ts +0 -727
- package/dist/wire/upload-pack.d.ts.map +0 -1
- package/dist/wire/upload-pack.js +0 -1141
- package/dist/wire/upload-pack.js.map +0 -1
package/dist/mcp/adapter.js
DELETED
|
@@ -1,895 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @fileoverview MCP (Model Context Protocol) SDK Adapter
|
|
3
|
-
*
|
|
4
|
-
* This module provides an adapter that bridges the MCP protocol to git operations,
|
|
5
|
-
* handling request/response, tool registration/invocation, resource listing,
|
|
6
|
-
* and error handling. It implements the JSON-RPC 2.0 specification for MCP
|
|
7
|
-
* communication.
|
|
8
|
-
*
|
|
9
|
-
* The adapter supports:
|
|
10
|
-
* - Tool registration and invocation with schema validation
|
|
11
|
-
* - Resource registration and reading
|
|
12
|
-
* - Prompt registration and retrieval
|
|
13
|
-
* - Standard and custom MCP error codes
|
|
14
|
-
* - Batch request processing
|
|
15
|
-
* - Capability negotiation
|
|
16
|
-
*
|
|
17
|
-
* @module mcp/adapter
|
|
18
|
-
*
|
|
19
|
-
* @example
|
|
20
|
-
* // Create and configure an MCP adapter
|
|
21
|
-
* import { createMCPAdapter, MCPAdapter } from './adapter'
|
|
22
|
-
*
|
|
23
|
-
* const adapter = createMCPAdapter({
|
|
24
|
-
* name: 'my-git-server',
|
|
25
|
-
* version: '1.0.0',
|
|
26
|
-
* capabilities: ['tools', 'resources']
|
|
27
|
-
* })
|
|
28
|
-
*
|
|
29
|
-
* // Register git tools and start
|
|
30
|
-
* adapter.registerGitTools()
|
|
31
|
-
* await adapter.start()
|
|
32
|
-
*
|
|
33
|
-
* // Handle incoming requests
|
|
34
|
-
* const response = await adapter.handleRequest({
|
|
35
|
-
* jsonrpc: '2.0',
|
|
36
|
-
* id: 1,
|
|
37
|
-
* method: 'tools/list',
|
|
38
|
-
* params: {}
|
|
39
|
-
* })
|
|
40
|
-
*
|
|
41
|
-
* @example
|
|
42
|
-
* // Handle raw JSON requests
|
|
43
|
-
* const rawResponse = await adapter.handleRawRequest(
|
|
44
|
-
* '{"jsonrpc":"2.0","id":1,"method":"initialize","params":{}}'
|
|
45
|
-
* )
|
|
46
|
-
*/
|
|
47
|
-
import { gitTools } from './tools';
|
|
48
|
-
/**
|
|
49
|
-
* JSON-RPC 2.0 error codes and MCP-specific error codes.
|
|
50
|
-
*
|
|
51
|
-
* @description
|
|
52
|
-
* Enumeration of error codes used in MCP responses. Includes standard
|
|
53
|
-
* JSON-RPC 2.0 error codes (negative 32xxx range) and MCP-specific
|
|
54
|
-
* error codes for resource, tool, and prompt operations.
|
|
55
|
-
*
|
|
56
|
-
* @enum {number}
|
|
57
|
-
*
|
|
58
|
-
* @example
|
|
59
|
-
* // Using error codes in responses
|
|
60
|
-
* if (!tool) {
|
|
61
|
-
* return {
|
|
62
|
-
* jsonrpc: '2.0',
|
|
63
|
-
* id: requestId,
|
|
64
|
-
* error: {
|
|
65
|
-
* code: MCPErrorCode.TOOL_NOT_FOUND,
|
|
66
|
-
* message: 'Tool not found'
|
|
67
|
-
* }
|
|
68
|
-
* }
|
|
69
|
-
* }
|
|
70
|
-
*/
|
|
71
|
-
export var MCPErrorCode;
|
|
72
|
-
(function (MCPErrorCode) {
|
|
73
|
-
/** Parse error - Invalid JSON was received (-32700) */
|
|
74
|
-
MCPErrorCode[MCPErrorCode["PARSE_ERROR"] = -32700] = "PARSE_ERROR";
|
|
75
|
-
/** Invalid Request - The JSON sent is not a valid Request object (-32600) */
|
|
76
|
-
MCPErrorCode[MCPErrorCode["INVALID_REQUEST"] = -32600] = "INVALID_REQUEST";
|
|
77
|
-
/** Method not found - The method does not exist or is not available (-32601) */
|
|
78
|
-
MCPErrorCode[MCPErrorCode["METHOD_NOT_FOUND"] = -32601] = "METHOD_NOT_FOUND";
|
|
79
|
-
/** Invalid params - Invalid method parameter(s) (-32602) */
|
|
80
|
-
MCPErrorCode[MCPErrorCode["INVALID_PARAMS"] = -32602] = "INVALID_PARAMS";
|
|
81
|
-
/** Internal error - Internal JSON-RPC error (-32603) */
|
|
82
|
-
MCPErrorCode[MCPErrorCode["INTERNAL_ERROR"] = -32603] = "INTERNAL_ERROR";
|
|
83
|
-
/** Resource not found - The requested resource does not exist (-32001) */
|
|
84
|
-
MCPErrorCode[MCPErrorCode["RESOURCE_NOT_FOUND"] = -32001] = "RESOURCE_NOT_FOUND";
|
|
85
|
-
/** Tool not found - Maps to METHOD_NOT_FOUND as tools are methods (-32601) */
|
|
86
|
-
MCPErrorCode[MCPErrorCode["TOOL_NOT_FOUND"] = -32601] = "TOOL_NOT_FOUND";
|
|
87
|
-
/** Prompt not found - The requested prompt does not exist (-32003) */
|
|
88
|
-
MCPErrorCode[MCPErrorCode["PROMPT_NOT_FOUND"] = -32003] = "PROMPT_NOT_FOUND";
|
|
89
|
-
/** Capability not supported - The requested capability is not enabled (-32004) */
|
|
90
|
-
MCPErrorCode[MCPErrorCode["CAPABILITY_NOT_SUPPORTED"] = -32004] = "CAPABILITY_NOT_SUPPORTED";
|
|
91
|
-
})(MCPErrorCode || (MCPErrorCode = {}));
|
|
92
|
-
/**
|
|
93
|
-
* Custom error class for MCP errors.
|
|
94
|
-
*
|
|
95
|
-
* @description
|
|
96
|
-
* Error class that encapsulates MCP error information including a numeric
|
|
97
|
-
* error code, human-readable message, and optional additional data. Can be
|
|
98
|
-
* serialized to JSON-RPC error format using the toJSON() method.
|
|
99
|
-
*
|
|
100
|
-
* @class MCPError
|
|
101
|
-
* @extends Error
|
|
102
|
-
*
|
|
103
|
-
* @example
|
|
104
|
-
* // Throw an MCP error
|
|
105
|
-
* throw new MCPError(
|
|
106
|
-
* MCPErrorCode.TOOL_NOT_FOUND,
|
|
107
|
-
* 'Tool "unknown_tool" not found',
|
|
108
|
-
* { toolName: 'unknown_tool' }
|
|
109
|
-
* )
|
|
110
|
-
*
|
|
111
|
-
* @example
|
|
112
|
-
* // Convert to JSON-RPC error format
|
|
113
|
-
* const error = new MCPError(MCPErrorCode.INVALID_PARAMS, 'Missing required field')
|
|
114
|
-
* const jsonError = error.toJSON()
|
|
115
|
-
* // { code: -32602, message: 'Missing required field' }
|
|
116
|
-
*/
|
|
117
|
-
export class MCPError extends Error {
|
|
118
|
-
/** The MCP error code */
|
|
119
|
-
code;
|
|
120
|
-
/** Optional additional error data */
|
|
121
|
-
data;
|
|
122
|
-
/**
|
|
123
|
-
* Create a new MCP error.
|
|
124
|
-
*
|
|
125
|
-
* @param code - The MCP error code
|
|
126
|
-
* @param message - Human-readable error message
|
|
127
|
-
* @param data - Optional additional error data
|
|
128
|
-
*/
|
|
129
|
-
constructor(code, message, data) {
|
|
130
|
-
super(message);
|
|
131
|
-
this.code = code;
|
|
132
|
-
this.data = data;
|
|
133
|
-
this.name = 'MCPError';
|
|
134
|
-
}
|
|
135
|
-
/**
|
|
136
|
-
* Convert the error to JSON-RPC error format.
|
|
137
|
-
*
|
|
138
|
-
* @returns Object suitable for JSON-RPC error responses
|
|
139
|
-
*/
|
|
140
|
-
toJSON() {
|
|
141
|
-
const result = {
|
|
142
|
-
code: this.code,
|
|
143
|
-
message: this.message,
|
|
144
|
-
};
|
|
145
|
-
if (this.data !== undefined) {
|
|
146
|
-
result.data = this.data;
|
|
147
|
-
}
|
|
148
|
-
return result;
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
/**
|
|
152
|
-
* MCP Adapter class that bridges MCP protocol to git operations.
|
|
153
|
-
*
|
|
154
|
-
* @description
|
|
155
|
-
* The main adapter class that handles MCP protocol communication. It manages
|
|
156
|
-
* tool, resource, and prompt registrations, processes JSON-RPC requests,
|
|
157
|
-
* and returns properly formatted responses.
|
|
158
|
-
*
|
|
159
|
-
* The adapter supports the following MCP methods:
|
|
160
|
-
* - initialize: Server initialization and capability negotiation
|
|
161
|
-
* - tools/list: List all registered tools
|
|
162
|
-
* - tools/call: Invoke a registered tool
|
|
163
|
-
* - resources/list: List all registered resources
|
|
164
|
-
* - resources/read: Read a resource's content
|
|
165
|
-
* - prompts/list: List all registered prompts
|
|
166
|
-
* - prompts/get: Get a prompt's generated messages
|
|
167
|
-
*
|
|
168
|
-
* @class MCPAdapter
|
|
169
|
-
*
|
|
170
|
-
* @example
|
|
171
|
-
* // Create and use an adapter
|
|
172
|
-
* const adapter = new MCPAdapter({
|
|
173
|
-
* name: 'my-server',
|
|
174
|
-
* version: '1.0.0',
|
|
175
|
-
* capabilities: ['tools']
|
|
176
|
-
* })
|
|
177
|
-
*
|
|
178
|
-
* adapter.registerGitTools()
|
|
179
|
-
* await adapter.start()
|
|
180
|
-
*
|
|
181
|
-
* const response = await adapter.handleRequest({
|
|
182
|
-
* jsonrpc: '2.0',
|
|
183
|
-
* id: 1,
|
|
184
|
-
* method: 'tools/list'
|
|
185
|
-
* })
|
|
186
|
-
*/
|
|
187
|
-
export class MCPAdapter {
|
|
188
|
-
/** @internal */
|
|
189
|
-
config;
|
|
190
|
-
/** @internal */
|
|
191
|
-
initialized = false;
|
|
192
|
-
/** @internal */
|
|
193
|
-
tools = new Map();
|
|
194
|
-
/** @internal */
|
|
195
|
-
resources = new Map();
|
|
196
|
-
/** @internal */
|
|
197
|
-
prompts = new Map();
|
|
198
|
-
/**
|
|
199
|
-
* Create a new MCP adapter instance.
|
|
200
|
-
*
|
|
201
|
-
* @param config - Optional configuration options
|
|
202
|
-
*
|
|
203
|
-
* @example
|
|
204
|
-
* const adapter = new MCPAdapter({
|
|
205
|
-
* name: 'git-mcp-server',
|
|
206
|
-
* version: '2.0.0',
|
|
207
|
-
* capabilities: ['tools', 'resources', 'prompts']
|
|
208
|
-
* })
|
|
209
|
-
*/
|
|
210
|
-
constructor(config) {
|
|
211
|
-
this.config = {
|
|
212
|
-
name: config?.name || 'gitx.do',
|
|
213
|
-
version: config?.version || '1.0.0',
|
|
214
|
-
capabilities: config?.capabilities || ['tools'],
|
|
215
|
-
};
|
|
216
|
-
}
|
|
217
|
-
/**
|
|
218
|
-
* Get the server configuration.
|
|
219
|
-
*
|
|
220
|
-
* @description
|
|
221
|
-
* Returns a copy of the current server configuration including name,
|
|
222
|
-
* version, and enabled capabilities.
|
|
223
|
-
*
|
|
224
|
-
* @returns A copy of the server configuration
|
|
225
|
-
*
|
|
226
|
-
* @example
|
|
227
|
-
* const config = adapter.getConfig()
|
|
228
|
-
* console.log(`Server: ${config.name} v${config.version}`)
|
|
229
|
-
*/
|
|
230
|
-
getConfig() {
|
|
231
|
-
return { ...this.config };
|
|
232
|
-
}
|
|
233
|
-
/**
|
|
234
|
-
* Check if adapter has a specific capability.
|
|
235
|
-
*
|
|
236
|
-
* @description
|
|
237
|
-
* Tests whether a specific capability is enabled for this adapter.
|
|
238
|
-
* Used internally to determine which methods are available.
|
|
239
|
-
*
|
|
240
|
-
* @param capability - The capability to check ('tools', 'resources', or 'prompts')
|
|
241
|
-
* @returns True if the capability is enabled
|
|
242
|
-
*
|
|
243
|
-
* @example
|
|
244
|
-
* if (adapter.hasCapability('resources')) {
|
|
245
|
-
* adapter.registerResource(myResource)
|
|
246
|
-
* }
|
|
247
|
-
*/
|
|
248
|
-
hasCapability(capability) {
|
|
249
|
-
return this.config.capabilities.includes(capability);
|
|
250
|
-
}
|
|
251
|
-
/**
|
|
252
|
-
* Check if the adapter is initialized.
|
|
253
|
-
*
|
|
254
|
-
* @description
|
|
255
|
-
* Returns whether the adapter has been started and is ready to handle requests.
|
|
256
|
-
*
|
|
257
|
-
* @returns True if the adapter is initialized and running
|
|
258
|
-
*/
|
|
259
|
-
isInitialized() {
|
|
260
|
-
return this.initialized;
|
|
261
|
-
}
|
|
262
|
-
/**
|
|
263
|
-
* Start the MCP adapter.
|
|
264
|
-
*
|
|
265
|
-
* @description
|
|
266
|
-
* Initializes the adapter and prepares it to handle requests.
|
|
267
|
-
* Must be called before processing any MCP requests.
|
|
268
|
-
*
|
|
269
|
-
* @returns Promise that resolves when the adapter is started
|
|
270
|
-
* @throws {Error} If the adapter is already started
|
|
271
|
-
*
|
|
272
|
-
* @example
|
|
273
|
-
* const adapter = new MCPAdapter()
|
|
274
|
-
* await adapter.start()
|
|
275
|
-
* // Adapter is now ready to handle requests
|
|
276
|
-
*/
|
|
277
|
-
async start() {
|
|
278
|
-
if (this.initialized) {
|
|
279
|
-
throw new Error('MCP adapter is already initialized/started');
|
|
280
|
-
}
|
|
281
|
-
this.initialized = true;
|
|
282
|
-
}
|
|
283
|
-
/**
|
|
284
|
-
* Stop the MCP adapter.
|
|
285
|
-
*
|
|
286
|
-
* @description
|
|
287
|
-
* Shuts down the adapter and clears all registered tools, resources,
|
|
288
|
-
* and prompts. After stopping, the adapter must be restarted before
|
|
289
|
-
* handling new requests.
|
|
290
|
-
*
|
|
291
|
-
* @returns Promise that resolves when the adapter is stopped
|
|
292
|
-
* @throws {Error} If the adapter is not currently running
|
|
293
|
-
*
|
|
294
|
-
* @example
|
|
295
|
-
* await adapter.stop()
|
|
296
|
-
* // All registrations are cleared
|
|
297
|
-
*/
|
|
298
|
-
async stop() {
|
|
299
|
-
if (!this.initialized) {
|
|
300
|
-
throw new Error('MCP adapter is not initialized/not started');
|
|
301
|
-
}
|
|
302
|
-
this.initialized = false;
|
|
303
|
-
this.tools.clear();
|
|
304
|
-
this.resources.clear();
|
|
305
|
-
this.prompts.clear();
|
|
306
|
-
}
|
|
307
|
-
/**
|
|
308
|
-
* Register a tool.
|
|
309
|
-
*
|
|
310
|
-
* @description
|
|
311
|
-
* Adds a tool to the adapter's registry. The tool will be available
|
|
312
|
-
* for listing via tools/list and invocation via tools/call.
|
|
313
|
-
*
|
|
314
|
-
* @param toolInfo - The tool definition to register
|
|
315
|
-
* @returns void
|
|
316
|
-
* @throws {Error} If a tool with the same name is already registered
|
|
317
|
-
*
|
|
318
|
-
* @example
|
|
319
|
-
* adapter.registerTool({
|
|
320
|
-
* name: 'my_tool',
|
|
321
|
-
* description: 'Does something',
|
|
322
|
-
* inputSchema: { type: 'object', properties: {} },
|
|
323
|
-
* handler: async (params) => ({
|
|
324
|
-
* content: [{ type: 'text', text: 'Done' }]
|
|
325
|
-
* })
|
|
326
|
-
* })
|
|
327
|
-
*/
|
|
328
|
-
registerTool(toolInfo) {
|
|
329
|
-
if (this.tools.has(toolInfo.name)) {
|
|
330
|
-
throw new Error(`Tool '${toolInfo.name}' is already registered (duplicate)`);
|
|
331
|
-
}
|
|
332
|
-
this.tools.set(toolInfo.name, toolInfo);
|
|
333
|
-
}
|
|
334
|
-
/**
|
|
335
|
-
* Unregister a tool by name.
|
|
336
|
-
*
|
|
337
|
-
* @description
|
|
338
|
-
* Removes a tool from the adapter's registry. The tool will no longer
|
|
339
|
-
* be available for listing or invocation.
|
|
340
|
-
*
|
|
341
|
-
* @param name - The name of the tool to unregister
|
|
342
|
-
* @returns void
|
|
343
|
-
* @throws {Error} If no tool with the given name exists
|
|
344
|
-
*
|
|
345
|
-
* @example
|
|
346
|
-
* adapter.unregisterTool('my_tool')
|
|
347
|
-
*/
|
|
348
|
-
unregisterTool(name) {
|
|
349
|
-
if (!this.tools.has(name)) {
|
|
350
|
-
throw new Error(`Tool '${name}' not found (does not exist)`);
|
|
351
|
-
}
|
|
352
|
-
this.tools.delete(name);
|
|
353
|
-
}
|
|
354
|
-
/**
|
|
355
|
-
* List all registered tools (without handlers).
|
|
356
|
-
*
|
|
357
|
-
* @description
|
|
358
|
-
* Returns an array of all registered tools with their metadata.
|
|
359
|
-
* Handler functions are omitted for serialization safety.
|
|
360
|
-
*
|
|
361
|
-
* @returns Array of tool definitions without handlers
|
|
362
|
-
*
|
|
363
|
-
* @example
|
|
364
|
-
* const tools = adapter.listTools()
|
|
365
|
-
* for (const tool of tools) {
|
|
366
|
-
* console.log(`${tool.name}: ${tool.description}`)
|
|
367
|
-
* }
|
|
368
|
-
*/
|
|
369
|
-
listTools() {
|
|
370
|
-
const result = [];
|
|
371
|
-
for (const tool of this.tools.values()) {
|
|
372
|
-
result.push({
|
|
373
|
-
name: tool.name,
|
|
374
|
-
description: tool.description,
|
|
375
|
-
inputSchema: tool.inputSchema,
|
|
376
|
-
});
|
|
377
|
-
}
|
|
378
|
-
return result;
|
|
379
|
-
}
|
|
380
|
-
/**
|
|
381
|
-
* Get a tool by name (without handler).
|
|
382
|
-
*
|
|
383
|
-
* @description
|
|
384
|
-
* Retrieves a single tool's metadata by name. Returns undefined if
|
|
385
|
-
* the tool is not found.
|
|
386
|
-
*
|
|
387
|
-
* @param name - The name of the tool to retrieve
|
|
388
|
-
* @returns The tool definition without handler, or undefined if not found
|
|
389
|
-
*
|
|
390
|
-
* @example
|
|
391
|
-
* const tool = adapter.getTool('git_status')
|
|
392
|
-
* if (tool) {
|
|
393
|
-
* console.log(tool.description)
|
|
394
|
-
* }
|
|
395
|
-
*/
|
|
396
|
-
getTool(name) {
|
|
397
|
-
const tool = this.tools.get(name);
|
|
398
|
-
if (!tool)
|
|
399
|
-
return undefined;
|
|
400
|
-
return {
|
|
401
|
-
name: tool.name,
|
|
402
|
-
description: tool.description,
|
|
403
|
-
inputSchema: tool.inputSchema,
|
|
404
|
-
};
|
|
405
|
-
}
|
|
406
|
-
/**
|
|
407
|
-
* Register all git tools.
|
|
408
|
-
*
|
|
409
|
-
* @description
|
|
410
|
-
* Convenience method that registers all built-in git tools from the
|
|
411
|
-
* tools module. Skips any tools that are already registered.
|
|
412
|
-
*
|
|
413
|
-
* @returns void
|
|
414
|
-
*
|
|
415
|
-
* @example
|
|
416
|
-
* const adapter = new MCPAdapter()
|
|
417
|
-
* adapter.registerGitTools()
|
|
418
|
-
* // All 18 git tools are now registered
|
|
419
|
-
*/
|
|
420
|
-
registerGitTools() {
|
|
421
|
-
for (const tool of gitTools) {
|
|
422
|
-
if (!this.tools.has(tool.name)) {
|
|
423
|
-
this.registerTool({
|
|
424
|
-
name: tool.name,
|
|
425
|
-
description: tool.description,
|
|
426
|
-
inputSchema: tool.inputSchema,
|
|
427
|
-
handler: tool.handler,
|
|
428
|
-
});
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
/**
|
|
433
|
-
* Register a resource.
|
|
434
|
-
*
|
|
435
|
-
* @description
|
|
436
|
-
* Adds a resource to the adapter's registry. The resource will be
|
|
437
|
-
* available for listing and reading via the resources/* methods.
|
|
438
|
-
*
|
|
439
|
-
* @param resourceInfo - The resource definition to register
|
|
440
|
-
* @returns void
|
|
441
|
-
*
|
|
442
|
-
* @example
|
|
443
|
-
* adapter.registerResource({
|
|
444
|
-
* uri: 'git://repo/config',
|
|
445
|
-
* name: 'Repository Config',
|
|
446
|
-
* mimeType: 'application/json',
|
|
447
|
-
* handler: async () => ({ content: JSON.stringify(config) })
|
|
448
|
-
* })
|
|
449
|
-
*/
|
|
450
|
-
registerResource(resourceInfo) {
|
|
451
|
-
this.resources.set(resourceInfo.uri, resourceInfo);
|
|
452
|
-
}
|
|
453
|
-
/**
|
|
454
|
-
* Register a prompt.
|
|
455
|
-
*
|
|
456
|
-
* @description
|
|
457
|
-
* Adds a prompt template to the adapter's registry. The prompt will
|
|
458
|
-
* be available for listing and retrieval via the prompts/* methods.
|
|
459
|
-
*
|
|
460
|
-
* @param promptInfo - The prompt definition to register
|
|
461
|
-
* @returns void
|
|
462
|
-
*
|
|
463
|
-
* @example
|
|
464
|
-
* adapter.registerPrompt({
|
|
465
|
-
* name: 'review-code',
|
|
466
|
-
* description: 'Review code changes',
|
|
467
|
-
* handler: async () => ({
|
|
468
|
-
* messages: [{ role: 'user', content: { type: 'text', text: '...' } }]
|
|
469
|
-
* })
|
|
470
|
-
* })
|
|
471
|
-
*/
|
|
472
|
-
registerPrompt(promptInfo) {
|
|
473
|
-
this.prompts.set(promptInfo.name, promptInfo);
|
|
474
|
-
}
|
|
475
|
-
/**
|
|
476
|
-
* Handle a raw JSON string request.
|
|
477
|
-
*
|
|
478
|
-
* @description
|
|
479
|
-
* Parses a raw JSON string as an MCP request and processes it.
|
|
480
|
-
* Returns a parse error response if the JSON is invalid.
|
|
481
|
-
*
|
|
482
|
-
* @param rawRequest - Raw JSON string containing the request
|
|
483
|
-
* @returns Promise resolving to the MCP response
|
|
484
|
-
*
|
|
485
|
-
* @example
|
|
486
|
-
* const response = await adapter.handleRawRequest(
|
|
487
|
-
* '{"jsonrpc":"2.0","id":1,"method":"tools/list"}'
|
|
488
|
-
* )
|
|
489
|
-
*/
|
|
490
|
-
async handleRawRequest(rawRequest) {
|
|
491
|
-
let request;
|
|
492
|
-
try {
|
|
493
|
-
request = JSON.parse(rawRequest);
|
|
494
|
-
}
|
|
495
|
-
catch {
|
|
496
|
-
return {
|
|
497
|
-
jsonrpc: '2.0',
|
|
498
|
-
error: {
|
|
499
|
-
code: MCPErrorCode.PARSE_ERROR,
|
|
500
|
-
message: 'Parse error: Invalid JSON',
|
|
501
|
-
},
|
|
502
|
-
};
|
|
503
|
-
}
|
|
504
|
-
const response = await this.handleRequest(request);
|
|
505
|
-
return response ?? {
|
|
506
|
-
jsonrpc: '2.0',
|
|
507
|
-
error: {
|
|
508
|
-
code: MCPErrorCode.INVALID_REQUEST,
|
|
509
|
-
message: 'Invalid Request: notification without id',
|
|
510
|
-
},
|
|
511
|
-
};
|
|
512
|
-
}
|
|
513
|
-
/**
|
|
514
|
-
* Handle a batch of requests.
|
|
515
|
-
*
|
|
516
|
-
* @description
|
|
517
|
-
* Processes multiple MCP requests sequentially. Notifications (requests
|
|
518
|
-
* without an id) are processed but do not produce responses.
|
|
519
|
-
*
|
|
520
|
-
* @param requests - Array of MCP requests to process
|
|
521
|
-
* @returns Promise resolving to array of responses (excluding notifications)
|
|
522
|
-
*
|
|
523
|
-
* @example
|
|
524
|
-
* const responses = await adapter.handleBatchRequest([
|
|
525
|
-
* { jsonrpc: '2.0', id: 1, method: 'tools/list' },
|
|
526
|
-
* { jsonrpc: '2.0', id: 2, method: 'initialize', params: {} }
|
|
527
|
-
* ])
|
|
528
|
-
*/
|
|
529
|
-
async handleBatchRequest(requests) {
|
|
530
|
-
const responses = [];
|
|
531
|
-
for (const request of requests) {
|
|
532
|
-
const response = await this.handleRequest(request);
|
|
533
|
-
// Only include responses for requests with id (not notifications)
|
|
534
|
-
if (response !== undefined) {
|
|
535
|
-
responses.push(response);
|
|
536
|
-
}
|
|
537
|
-
}
|
|
538
|
-
return responses;
|
|
539
|
-
}
|
|
540
|
-
/**
|
|
541
|
-
* Handle a single MCP request.
|
|
542
|
-
*
|
|
543
|
-
* @description
|
|
544
|
-
* Main request handler that routes MCP requests to the appropriate
|
|
545
|
-
* method handler. Supports initialize, tools/*, resources/*, and prompts/*
|
|
546
|
-
* methods. Returns undefined for notifications (requests without id).
|
|
547
|
-
*
|
|
548
|
-
* @param request - The MCP request to handle
|
|
549
|
-
* @returns Promise resolving to response, or undefined for notifications
|
|
550
|
-
*
|
|
551
|
-
* @example
|
|
552
|
-
* const response = await adapter.handleRequest({
|
|
553
|
-
* jsonrpc: '2.0',
|
|
554
|
-
* id: 1,
|
|
555
|
-
* method: 'tools/call',
|
|
556
|
-
* params: { name: 'git_status', arguments: {} }
|
|
557
|
-
* })
|
|
558
|
-
*/
|
|
559
|
-
async handleRequest(request) {
|
|
560
|
-
// Handle notifications (no id) - they don't expect a response
|
|
561
|
-
if (request.id === undefined) {
|
|
562
|
-
// Process notification but don't return a response
|
|
563
|
-
return undefined;
|
|
564
|
-
}
|
|
565
|
-
// Validate jsonrpc version
|
|
566
|
-
if (request.jsonrpc !== '2.0') {
|
|
567
|
-
return this.errorResponse(request.id, MCPErrorCode.INVALID_REQUEST, 'Invalid Request: missing or invalid jsonrpc version');
|
|
568
|
-
}
|
|
569
|
-
try {
|
|
570
|
-
// Route to appropriate handler based on method
|
|
571
|
-
switch (request.method) {
|
|
572
|
-
case 'initialize':
|
|
573
|
-
return this.handleInitialize(request);
|
|
574
|
-
case 'tools/list':
|
|
575
|
-
return this.handleToolsList(request);
|
|
576
|
-
case 'tools/call':
|
|
577
|
-
return this.handleToolsCall(request);
|
|
578
|
-
case 'resources/list':
|
|
579
|
-
return this.handleResourcesList(request);
|
|
580
|
-
case 'resources/read':
|
|
581
|
-
return this.handleResourcesRead(request);
|
|
582
|
-
case 'prompts/list':
|
|
583
|
-
return this.handlePromptsList(request);
|
|
584
|
-
case 'prompts/get':
|
|
585
|
-
return this.handlePromptsGet(request);
|
|
586
|
-
default:
|
|
587
|
-
return this.errorResponse(request.id, MCPErrorCode.METHOD_NOT_FOUND, `Method not found: ${request.method}`);
|
|
588
|
-
}
|
|
589
|
-
}
|
|
590
|
-
catch (error) {
|
|
591
|
-
if (error instanceof MCPError) {
|
|
592
|
-
return this.errorResponse(request.id, error.code, error.message, error.data);
|
|
593
|
-
}
|
|
594
|
-
return this.errorResponse(request.id, MCPErrorCode.INTERNAL_ERROR, error instanceof Error ? error.message : 'Internal error');
|
|
595
|
-
}
|
|
596
|
-
}
|
|
597
|
-
/**
|
|
598
|
-
* Handle initialize request.
|
|
599
|
-
*
|
|
600
|
-
* @description
|
|
601
|
-
* Processes the MCP initialize request and returns server information
|
|
602
|
-
* and capabilities. This is the first request a client should send.
|
|
603
|
-
*
|
|
604
|
-
* @param request - The initialize request
|
|
605
|
-
* @returns Response with server info and capabilities
|
|
606
|
-
* @internal
|
|
607
|
-
*/
|
|
608
|
-
handleInitialize(request) {
|
|
609
|
-
const params = request.params || {};
|
|
610
|
-
const protocolVersion = params.protocolVersion || '2024-11-05';
|
|
611
|
-
const capabilities = {};
|
|
612
|
-
if (this.hasCapability('tools')) {
|
|
613
|
-
capabilities.tools = {};
|
|
614
|
-
}
|
|
615
|
-
if (this.hasCapability('resources')) {
|
|
616
|
-
capabilities.resources = {};
|
|
617
|
-
}
|
|
618
|
-
if (this.hasCapability('prompts')) {
|
|
619
|
-
capabilities.prompts = {};
|
|
620
|
-
}
|
|
621
|
-
return {
|
|
622
|
-
jsonrpc: '2.0',
|
|
623
|
-
id: request.id,
|
|
624
|
-
result: {
|
|
625
|
-
protocolVersion,
|
|
626
|
-
serverInfo: {
|
|
627
|
-
name: this.config.name,
|
|
628
|
-
version: this.config.version,
|
|
629
|
-
},
|
|
630
|
-
capabilities,
|
|
631
|
-
},
|
|
632
|
-
};
|
|
633
|
-
}
|
|
634
|
-
/**
|
|
635
|
-
* Handle tools/list request.
|
|
636
|
-
* @param request - The tools/list request
|
|
637
|
-
* @returns Response with list of registered tools
|
|
638
|
-
* @internal
|
|
639
|
-
*/
|
|
640
|
-
handleToolsList(request) {
|
|
641
|
-
if (!this.hasCapability('tools')) {
|
|
642
|
-
return this.errorResponse(request.id, MCPErrorCode.CAPABILITY_NOT_SUPPORTED, 'Tools capability is not supported');
|
|
643
|
-
}
|
|
644
|
-
return {
|
|
645
|
-
jsonrpc: '2.0',
|
|
646
|
-
id: request.id,
|
|
647
|
-
result: {
|
|
648
|
-
tools: this.listTools(),
|
|
649
|
-
},
|
|
650
|
-
};
|
|
651
|
-
}
|
|
652
|
-
/**
|
|
653
|
-
* Handle tools/call request.
|
|
654
|
-
* @param request - The tools/call request with tool name and arguments
|
|
655
|
-
* @returns Response with tool execution result
|
|
656
|
-
* @internal
|
|
657
|
-
*/
|
|
658
|
-
async handleToolsCall(request) {
|
|
659
|
-
if (!this.hasCapability('tools')) {
|
|
660
|
-
return this.errorResponse(request.id, MCPErrorCode.CAPABILITY_NOT_SUPPORTED, 'Tools capability is not supported');
|
|
661
|
-
}
|
|
662
|
-
const params = request.params || {};
|
|
663
|
-
const toolName = params.name;
|
|
664
|
-
const toolArgs = (params.arguments || {});
|
|
665
|
-
const tool = this.tools.get(toolName);
|
|
666
|
-
if (!tool) {
|
|
667
|
-
// Use TOOL_NOT_FOUND (which equals METHOD_NOT_FOUND) for non-existent tools
|
|
668
|
-
return this.errorResponse(request.id, MCPErrorCode.TOOL_NOT_FOUND, `Tool '${toolName}' not found (does not exist)`);
|
|
669
|
-
}
|
|
670
|
-
// Validate parameters
|
|
671
|
-
const validation = this.validateToolParams(tool, toolArgs);
|
|
672
|
-
if (!validation.valid) {
|
|
673
|
-
return this.errorResponse(request.id, MCPErrorCode.INVALID_PARAMS, validation.errors.join('; '));
|
|
674
|
-
}
|
|
675
|
-
// Execute tool
|
|
676
|
-
try {
|
|
677
|
-
const result = await tool.handler(toolArgs);
|
|
678
|
-
return {
|
|
679
|
-
jsonrpc: '2.0',
|
|
680
|
-
id: request.id,
|
|
681
|
-
result,
|
|
682
|
-
};
|
|
683
|
-
}
|
|
684
|
-
catch (error) {
|
|
685
|
-
// Tool execution errors are returned as successful responses with isError flag
|
|
686
|
-
return {
|
|
687
|
-
jsonrpc: '2.0',
|
|
688
|
-
id: request.id,
|
|
689
|
-
result: {
|
|
690
|
-
content: [
|
|
691
|
-
{
|
|
692
|
-
type: 'text',
|
|
693
|
-
text: error instanceof Error ? error.message : String(error),
|
|
694
|
-
},
|
|
695
|
-
],
|
|
696
|
-
isError: true,
|
|
697
|
-
},
|
|
698
|
-
};
|
|
699
|
-
}
|
|
700
|
-
}
|
|
701
|
-
/**
|
|
702
|
-
* Validate tool parameters against schema.
|
|
703
|
-
* @param tool - The tool to validate parameters for
|
|
704
|
-
* @param params - The parameters to validate
|
|
705
|
-
* @returns Validation result with errors array
|
|
706
|
-
* @internal
|
|
707
|
-
*/
|
|
708
|
-
validateToolParams(tool, params) {
|
|
709
|
-
const errors = [];
|
|
710
|
-
const schema = tool.inputSchema;
|
|
711
|
-
// Check required parameters
|
|
712
|
-
if (schema.required) {
|
|
713
|
-
for (const requiredParam of schema.required) {
|
|
714
|
-
if (!(requiredParam in params) || params[requiredParam] === undefined) {
|
|
715
|
-
errors.push(`Missing required parameter: ${requiredParam}`);
|
|
716
|
-
}
|
|
717
|
-
}
|
|
718
|
-
}
|
|
719
|
-
// Check parameter types and constraints
|
|
720
|
-
if (schema.properties) {
|
|
721
|
-
for (const [key, value] of Object.entries(params)) {
|
|
722
|
-
const propSchema = schema.properties[key];
|
|
723
|
-
if (!propSchema)
|
|
724
|
-
continue;
|
|
725
|
-
// Type validation
|
|
726
|
-
const expectedType = propSchema.type;
|
|
727
|
-
const valueType = Array.isArray(value) ? 'array' : typeof value;
|
|
728
|
-
if (expectedType && valueType !== expectedType) {
|
|
729
|
-
errors.push(`Parameter '${key}' has invalid type: expected ${expectedType}, got ${valueType}`);
|
|
730
|
-
}
|
|
731
|
-
// Pattern validation for strings
|
|
732
|
-
if (expectedType === 'string' &&
|
|
733
|
-
typeof value === 'string' &&
|
|
734
|
-
propSchema.pattern) {
|
|
735
|
-
const pattern = new RegExp(propSchema.pattern);
|
|
736
|
-
if (!pattern.test(value)) {
|
|
737
|
-
errors.push(`Parameter '${key}' does not match pattern: ${propSchema.pattern}`);
|
|
738
|
-
}
|
|
739
|
-
}
|
|
740
|
-
}
|
|
741
|
-
}
|
|
742
|
-
return { valid: errors.length === 0, errors };
|
|
743
|
-
}
|
|
744
|
-
/**
|
|
745
|
-
* Handle resources/list request.
|
|
746
|
-
* @param request - The resources/list request
|
|
747
|
-
* @returns Response with list of registered resources
|
|
748
|
-
* @internal
|
|
749
|
-
*/
|
|
750
|
-
handleResourcesList(request) {
|
|
751
|
-
if (!this.hasCapability('resources')) {
|
|
752
|
-
return this.errorResponse(request.id, MCPErrorCode.CAPABILITY_NOT_SUPPORTED, 'Resources capability is not supported');
|
|
753
|
-
}
|
|
754
|
-
const resources = Array.from(this.resources.values()).map((r) => ({
|
|
755
|
-
uri: r.uri,
|
|
756
|
-
name: r.name,
|
|
757
|
-
mimeType: r.mimeType,
|
|
758
|
-
description: r.description,
|
|
759
|
-
}));
|
|
760
|
-
return {
|
|
761
|
-
jsonrpc: '2.0',
|
|
762
|
-
id: request.id,
|
|
763
|
-
result: { resources },
|
|
764
|
-
};
|
|
765
|
-
}
|
|
766
|
-
/**
|
|
767
|
-
* Handle resources/read request.
|
|
768
|
-
* @param request - The resources/read request with URI
|
|
769
|
-
* @returns Response with resource content
|
|
770
|
-
* @internal
|
|
771
|
-
*/
|
|
772
|
-
async handleResourcesRead(request) {
|
|
773
|
-
if (!this.hasCapability('resources')) {
|
|
774
|
-
return this.errorResponse(request.id, MCPErrorCode.CAPABILITY_NOT_SUPPORTED, 'Resources capability is not supported');
|
|
775
|
-
}
|
|
776
|
-
const params = request.params || {};
|
|
777
|
-
const uri = params.uri;
|
|
778
|
-
const resource = this.resources.get(uri);
|
|
779
|
-
if (!resource) {
|
|
780
|
-
return this.errorResponse(request.id, MCPErrorCode.RESOURCE_NOT_FOUND, `Resource not found: ${uri}`);
|
|
781
|
-
}
|
|
782
|
-
let content = '';
|
|
783
|
-
if (resource.handler) {
|
|
784
|
-
const result = await resource.handler();
|
|
785
|
-
content = result.content;
|
|
786
|
-
}
|
|
787
|
-
return {
|
|
788
|
-
jsonrpc: '2.0',
|
|
789
|
-
id: request.id,
|
|
790
|
-
result: {
|
|
791
|
-
contents: [
|
|
792
|
-
{
|
|
793
|
-
uri: resource.uri,
|
|
794
|
-
mimeType: resource.mimeType,
|
|
795
|
-
text: content,
|
|
796
|
-
},
|
|
797
|
-
],
|
|
798
|
-
},
|
|
799
|
-
};
|
|
800
|
-
}
|
|
801
|
-
/**
|
|
802
|
-
* Handle prompts/list request.
|
|
803
|
-
* @param request - The prompts/list request
|
|
804
|
-
* @returns Response with list of registered prompts
|
|
805
|
-
* @internal
|
|
806
|
-
*/
|
|
807
|
-
handlePromptsList(request) {
|
|
808
|
-
if (!this.hasCapability('prompts')) {
|
|
809
|
-
return this.errorResponse(request.id, MCPErrorCode.CAPABILITY_NOT_SUPPORTED, 'Prompts capability is not supported');
|
|
810
|
-
}
|
|
811
|
-
const prompts = Array.from(this.prompts.values()).map((p) => ({
|
|
812
|
-
name: p.name,
|
|
813
|
-
description: p.description,
|
|
814
|
-
arguments: p.arguments,
|
|
815
|
-
}));
|
|
816
|
-
return {
|
|
817
|
-
jsonrpc: '2.0',
|
|
818
|
-
id: request.id,
|
|
819
|
-
result: { prompts },
|
|
820
|
-
};
|
|
821
|
-
}
|
|
822
|
-
/**
|
|
823
|
-
* Handle prompts/get request.
|
|
824
|
-
* @param request - The prompts/get request with name and arguments
|
|
825
|
-
* @returns Response with generated prompt messages
|
|
826
|
-
* @internal
|
|
827
|
-
*/
|
|
828
|
-
async handlePromptsGet(request) {
|
|
829
|
-
if (!this.hasCapability('prompts')) {
|
|
830
|
-
return this.errorResponse(request.id, MCPErrorCode.CAPABILITY_NOT_SUPPORTED, 'Prompts capability is not supported');
|
|
831
|
-
}
|
|
832
|
-
const params = request.params || {};
|
|
833
|
-
const name = params.name;
|
|
834
|
-
const args = (params.arguments || {});
|
|
835
|
-
const prompt = this.prompts.get(name);
|
|
836
|
-
if (!prompt) {
|
|
837
|
-
return this.errorResponse(request.id, MCPErrorCode.PROMPT_NOT_FOUND, `Prompt not found: ${name}`);
|
|
838
|
-
}
|
|
839
|
-
let messages = [];
|
|
840
|
-
if (prompt.handler) {
|
|
841
|
-
const result = await prompt.handler(args);
|
|
842
|
-
messages = result.messages;
|
|
843
|
-
}
|
|
844
|
-
return {
|
|
845
|
-
jsonrpc: '2.0',
|
|
846
|
-
id: request.id,
|
|
847
|
-
result: { messages },
|
|
848
|
-
};
|
|
849
|
-
}
|
|
850
|
-
/**
|
|
851
|
-
* Create an error response.
|
|
852
|
-
* @param id - Request ID
|
|
853
|
-
* @param code - Error code
|
|
854
|
-
* @param message - Error message
|
|
855
|
-
* @param data - Optional additional error data
|
|
856
|
-
* @returns Formatted error response
|
|
857
|
-
* @internal
|
|
858
|
-
*/
|
|
859
|
-
errorResponse(id, code, message, data) {
|
|
860
|
-
const response = {
|
|
861
|
-
jsonrpc: '2.0',
|
|
862
|
-
id,
|
|
863
|
-
error: { code, message },
|
|
864
|
-
};
|
|
865
|
-
if (data !== undefined) {
|
|
866
|
-
response.error.data = data;
|
|
867
|
-
}
|
|
868
|
-
return response;
|
|
869
|
-
}
|
|
870
|
-
}
|
|
871
|
-
/**
|
|
872
|
-
* Factory function to create an MCP adapter.
|
|
873
|
-
*
|
|
874
|
-
* @description
|
|
875
|
-
* Convenience function for creating a new MCP adapter instance.
|
|
876
|
-
* Equivalent to using `new MCPAdapter(config)`.
|
|
877
|
-
*
|
|
878
|
-
* @param config - Optional server configuration
|
|
879
|
-
* @returns A new MCPAdapter instance
|
|
880
|
-
*
|
|
881
|
-
* @example
|
|
882
|
-
* import { createMCPAdapter } from './adapter'
|
|
883
|
-
*
|
|
884
|
-
* const adapter = createMCPAdapter({
|
|
885
|
-
* name: 'my-git-server',
|
|
886
|
-
* capabilities: ['tools', 'resources']
|
|
887
|
-
* })
|
|
888
|
-
*
|
|
889
|
-
* adapter.registerGitTools()
|
|
890
|
-
* await adapter.start()
|
|
891
|
-
*/
|
|
892
|
-
export function createMCPAdapter(config) {
|
|
893
|
-
return new MCPAdapter(config);
|
|
894
|
-
}
|
|
895
|
-
//# sourceMappingURL=adapter.js.map
|