@kya-os/mcp-i 0.1.0-alpha.1.0 → 0.1.0-alpha.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +66 -90
- package/dist/__tests__/challenge-response.test.d.ts +5 -0
- package/dist/__tests__/challenge-response.test.d.ts.map +1 -0
- package/dist/__tests__/challenge-response.test.js +218 -0
- package/dist/__tests__/challenge-response.test.js.map +1 -0
- package/dist/__tests__/crypto.test.d.ts +5 -0
- package/dist/__tests__/crypto.test.d.ts.map +1 -0
- package/dist/__tests__/crypto.test.js +153 -0
- package/dist/__tests__/crypto.test.js.map +1 -0
- package/dist/auto-enhance.d.ts +41 -0
- package/dist/auto-enhance.d.ts.map +1 -0
- package/dist/auto-enhance.js +193 -0
- package/dist/auto-enhance.js.map +1 -0
- package/dist/auto-init.d.ts +12 -0
- package/dist/auto-init.d.ts.map +1 -0
- package/dist/auto-init.js +166 -0
- package/dist/auto-init.js.map +1 -0
- package/dist/auto.d.ts +13 -0
- package/dist/auto.d.ts.map +1 -0
- package/dist/auto.js +24 -0
- package/dist/auto.js.map +1 -0
- package/dist/crypto.d.ts +32 -0
- package/dist/crypto.d.ts.map +1 -0
- package/dist/crypto.js +117 -0
- package/dist/crypto.js.map +1 -0
- package/dist/index.d.ts +43 -28
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +208 -28
- package/dist/index.js.map +1 -1
- package/dist/patch.d.ts +22 -0
- package/dist/patch.d.ts.map +1 -0
- package/dist/patch.js +164 -0
- package/dist/patch.js.map +1 -0
- package/dist/transparent.d.ts +40 -0
- package/dist/transparent.d.ts.map +1 -0
- package/dist/transparent.js +167 -0
- package/dist/transparent.js.map +1 -0
- package/dist/types.d.ts +79 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +6 -0
- package/dist/types.js.map +1 -0
- package/package.json +19 -4
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Auto-enhancement module for transparent MCP-I integration
|
|
4
|
+
*
|
|
5
|
+
* This module automatically wraps MCP server instances to add identity
|
|
6
|
+
* without requiring any code changes or type modifications.
|
|
7
|
+
*/
|
|
8
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
+
exports.enhanceMCPServer = enhanceMCPServer;
|
|
10
|
+
exports.createMCPServerProxy = createMCPServerProxy;
|
|
11
|
+
exports.resolve = resolve;
|
|
12
|
+
exports.load = load;
|
|
13
|
+
exports.getCurrentIdentity = getCurrentIdentity;
|
|
14
|
+
exports.initializeIdentity = initializeIdentity;
|
|
15
|
+
const index_1 = require("./index");
|
|
16
|
+
// Store the singleton identity instance
|
|
17
|
+
let globalIdentity = null;
|
|
18
|
+
/**
|
|
19
|
+
* Wrap a response handler to automatically add MCP-I identity
|
|
20
|
+
*/
|
|
21
|
+
function wrapHandler(originalHandler, identity) {
|
|
22
|
+
return async function (...args) {
|
|
23
|
+
// Call the original handler
|
|
24
|
+
const response = await originalHandler.apply(this, args);
|
|
25
|
+
// If response is null/undefined, return as-is
|
|
26
|
+
if (!response)
|
|
27
|
+
return response;
|
|
28
|
+
// Add identity metadata without changing the response structure
|
|
29
|
+
// We use Object.defineProperty to make it non-enumerable
|
|
30
|
+
Object.defineProperty(response, '_mcp_identity', {
|
|
31
|
+
value: {
|
|
32
|
+
did: identity.did,
|
|
33
|
+
signature: '', // Will be filled below
|
|
34
|
+
timestamp: new Date().toISOString(),
|
|
35
|
+
conformanceLevel: 2
|
|
36
|
+
},
|
|
37
|
+
writable: true,
|
|
38
|
+
enumerable: false, // Hidden from JSON.stringify by default
|
|
39
|
+
configurable: true
|
|
40
|
+
});
|
|
41
|
+
// Sign the response content
|
|
42
|
+
const contentToSign = JSON.stringify({
|
|
43
|
+
...response,
|
|
44
|
+
_mcp_identity: {
|
|
45
|
+
did: identity.did,
|
|
46
|
+
timestamp: response._mcp_identity.timestamp,
|
|
47
|
+
conformanceLevel: 2
|
|
48
|
+
}
|
|
49
|
+
});
|
|
50
|
+
response._mcp_identity.signature = await identity.sign(contentToSign);
|
|
51
|
+
// Override toJSON to include _mcp_identity when serializing
|
|
52
|
+
const originalToJSON = response.toJSON;
|
|
53
|
+
response.toJSON = function () {
|
|
54
|
+
const result = originalToJSON ? originalToJSON.call(this) : { ...this };
|
|
55
|
+
result._mcp_identity = response._mcp_identity;
|
|
56
|
+
return result;
|
|
57
|
+
};
|
|
58
|
+
return response;
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Enhance an MCP server instance with automatic identity
|
|
63
|
+
*/
|
|
64
|
+
async function enhanceMCPServer(server) {
|
|
65
|
+
// Skip if already enhanced
|
|
66
|
+
if (server._mcpIdentityEnhanced) {
|
|
67
|
+
return server;
|
|
68
|
+
}
|
|
69
|
+
// Initialize identity if not already done
|
|
70
|
+
if (!globalIdentity) {
|
|
71
|
+
globalIdentity = await index_1.MCPIdentity.init();
|
|
72
|
+
}
|
|
73
|
+
// Wrap setRequestHandler if it exists
|
|
74
|
+
if (server.setRequestHandler) {
|
|
75
|
+
const originalSetRequestHandler = server.setRequestHandler.bind(server);
|
|
76
|
+
server.setRequestHandler = function (schema, handler) {
|
|
77
|
+
const wrappedHandler = wrapHandler(handler, globalIdentity);
|
|
78
|
+
return originalSetRequestHandler(schema, wrappedHandler);
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
// Wrap tool method if it exists
|
|
82
|
+
if (server.tool) {
|
|
83
|
+
const originalTool = server.tool.bind(server);
|
|
84
|
+
server.tool = function (name, schema, handler) {
|
|
85
|
+
const wrappedHandler = wrapHandler(handler, globalIdentity);
|
|
86
|
+
return originalTool(name, schema, wrappedHandler);
|
|
87
|
+
};
|
|
88
|
+
}
|
|
89
|
+
// Wrap resource method if it exists
|
|
90
|
+
if (server.resource) {
|
|
91
|
+
const originalResource = server.resource.bind(server);
|
|
92
|
+
server.resource = function (name, template, handler) {
|
|
93
|
+
const wrappedHandler = wrapHandler(handler, globalIdentity);
|
|
94
|
+
return originalResource(name, template, wrappedHandler);
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
// Wrap prompt method if it exists
|
|
98
|
+
if (server.prompt) {
|
|
99
|
+
const originalPrompt = server.prompt.bind(server);
|
|
100
|
+
server.prompt = function (name, schema, handler) {
|
|
101
|
+
const wrappedHandler = wrapHandler(handler, globalIdentity);
|
|
102
|
+
return originalPrompt(name, schema, wrappedHandler);
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
// Mark as enhanced
|
|
106
|
+
server._mcpIdentityEnhanced = true;
|
|
107
|
+
return server;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Proxy constructor for automatic enhancement
|
|
111
|
+
*/
|
|
112
|
+
function createMCPServerProxy(ServerClass) {
|
|
113
|
+
return new Proxy(ServerClass, {
|
|
114
|
+
construct(target, args) {
|
|
115
|
+
const instance = new target(...args);
|
|
116
|
+
// Enhance asynchronously after construction
|
|
117
|
+
setImmediate(async () => {
|
|
118
|
+
await enhanceMCPServer(instance);
|
|
119
|
+
});
|
|
120
|
+
return instance;
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Module loader hook for automatic enhancement
|
|
126
|
+
* This can be used with Node.js --loader flag
|
|
127
|
+
*/
|
|
128
|
+
async function resolve(specifier, context, defaultResolve) {
|
|
129
|
+
return defaultResolve(specifier, context);
|
|
130
|
+
}
|
|
131
|
+
async function load(url, context, defaultLoad) {
|
|
132
|
+
const result = await defaultLoad(url, context);
|
|
133
|
+
// Only process TypeScript/JavaScript files
|
|
134
|
+
if (!result.source || (!url.endsWith('.js') && !url.endsWith('.ts') && !url.endsWith('.mjs'))) {
|
|
135
|
+
return result;
|
|
136
|
+
}
|
|
137
|
+
const source = result.source.toString();
|
|
138
|
+
// Check if this imports MCP SDK
|
|
139
|
+
if (source.includes('@modelcontextprotocol/sdk')) {
|
|
140
|
+
// Inject our enhancement code
|
|
141
|
+
const enhancedSource = `
|
|
142
|
+
import { enhanceMCPServer } from '@kya-os/mcp-i/auto-enhance';
|
|
143
|
+
|
|
144
|
+
// Auto-enhance any Server or McpServer instances
|
|
145
|
+
const originalCode = ${JSON.stringify(source)};
|
|
146
|
+
const moduleExports = await import('data:text/javascript;base64,' + btoa(originalCode));
|
|
147
|
+
|
|
148
|
+
// Wrap server constructors
|
|
149
|
+
if (moduleExports.Server) {
|
|
150
|
+
const OriginalServer = moduleExports.Server;
|
|
151
|
+
moduleExports.Server = class extends OriginalServer {
|
|
152
|
+
constructor(...args) {
|
|
153
|
+
super(...args);
|
|
154
|
+
setImmediate(() => enhanceMCPServer(this));
|
|
155
|
+
}
|
|
156
|
+
};
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
if (moduleExports.McpServer) {
|
|
160
|
+
const OriginalMcpServer = moduleExports.McpServer;
|
|
161
|
+
moduleExports.McpServer = class extends OriginalMcpServer {
|
|
162
|
+
constructor(...args) {
|
|
163
|
+
super(...args);
|
|
164
|
+
setImmediate(() => enhanceMCPServer(this));
|
|
165
|
+
}
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
export * from moduleExports;
|
|
170
|
+
`;
|
|
171
|
+
return {
|
|
172
|
+
...result,
|
|
173
|
+
source: enhancedSource
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
return result;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Get the current identity instance
|
|
180
|
+
*/
|
|
181
|
+
function getCurrentIdentity() {
|
|
182
|
+
return globalIdentity;
|
|
183
|
+
}
|
|
184
|
+
/**
|
|
185
|
+
* Initialize identity explicitly (optional)
|
|
186
|
+
*/
|
|
187
|
+
async function initializeIdentity(options) {
|
|
188
|
+
if (!globalIdentity) {
|
|
189
|
+
globalIdentity = await index_1.MCPIdentity.init(options);
|
|
190
|
+
}
|
|
191
|
+
return globalIdentity;
|
|
192
|
+
}
|
|
193
|
+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,CAAC;QACpB,cAAc,GAAG,MAAM,mBAAW,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;IACnD,CAAC;IACD,OAAO,cAAc,CAAC;AACxB,CAAC"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Auto-initialization module for @kya-os/mcp-i
|
|
3
|
+
*
|
|
4
|
+
* This file provides automatic enhancement of MCP servers when imported.
|
|
5
|
+
* Just add: import '@kya-os/mcp-i/auto-init'
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Initialize auto-enhancement
|
|
9
|
+
*/
|
|
10
|
+
declare function initialize(): Promise<void>;
|
|
11
|
+
export { initialize as initializeMCPIdentity };
|
|
12
|
+
//# sourceMappingURL=auto-init.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auto-init.d.ts","sourceRoot":"","sources":["../src/auto-init.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAiHH;;GAEG;AACH,iBAAe,UAAU,kBAqBxB;AAMD,OAAO,EAAE,UAAU,IAAI,qBAAqB,EAAE,CAAC"}
|
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Auto-initialization module for @kya-os/mcp-i
|
|
4
|
+
*
|
|
5
|
+
* This file provides automatic enhancement of MCP servers when imported.
|
|
6
|
+
* Just add: import '@kya-os/mcp-i/auto-init'
|
|
7
|
+
*/
|
|
8
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
9
|
+
if (k2 === undefined) k2 = k;
|
|
10
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
11
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
12
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
13
|
+
}
|
|
14
|
+
Object.defineProperty(o, k2, desc);
|
|
15
|
+
}) : (function(o, m, k, k2) {
|
|
16
|
+
if (k2 === undefined) k2 = k;
|
|
17
|
+
o[k2] = m[k];
|
|
18
|
+
}));
|
|
19
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
20
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
21
|
+
}) : function(o, v) {
|
|
22
|
+
o["default"] = v;
|
|
23
|
+
});
|
|
24
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
25
|
+
var ownKeys = function(o) {
|
|
26
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
27
|
+
var ar = [];
|
|
28
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
29
|
+
return ar;
|
|
30
|
+
};
|
|
31
|
+
return ownKeys(o);
|
|
32
|
+
};
|
|
33
|
+
return function (mod) {
|
|
34
|
+
if (mod && mod.__esModule) return mod;
|
|
35
|
+
var result = {};
|
|
36
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
37
|
+
__setModuleDefault(result, mod);
|
|
38
|
+
return result;
|
|
39
|
+
};
|
|
40
|
+
})();
|
|
41
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
42
|
+
exports.initializeMCPIdentity = initialize;
|
|
43
|
+
const auto_enhance_1 = require("./auto-enhance");
|
|
44
|
+
// Track if we've already hooked into the module system
|
|
45
|
+
let isInitialized = false;
|
|
46
|
+
/**
|
|
47
|
+
* Hook into CommonJS require to automatically enhance MCP servers
|
|
48
|
+
*/
|
|
49
|
+
function hookRequire() {
|
|
50
|
+
const Module = require('module');
|
|
51
|
+
const originalRequire = Module.prototype.require;
|
|
52
|
+
Module.prototype.require = function (id) {
|
|
53
|
+
const exports = originalRequire.apply(this, arguments);
|
|
54
|
+
// Check if this is an MCP SDK module
|
|
55
|
+
if (id === '@modelcontextprotocol/sdk' ||
|
|
56
|
+
id === '@modelcontextprotocol/sdk/server' ||
|
|
57
|
+
id === '@modelcontextprotocol/sdk/server/index.js' ||
|
|
58
|
+
id === '@modelcontextprotocol/sdk/server/mcp.js') {
|
|
59
|
+
// Wrap Server constructor if it exists
|
|
60
|
+
if (exports.Server && !exports.Server._mcpIdentityWrapped) {
|
|
61
|
+
const OriginalServer = exports.Server;
|
|
62
|
+
exports.Server = class extends OriginalServer {
|
|
63
|
+
constructor(...args) {
|
|
64
|
+
super(...args);
|
|
65
|
+
// Enhance after construction
|
|
66
|
+
setImmediate(async () => {
|
|
67
|
+
await (0, auto_enhance_1.enhanceMCPServer)(this);
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
exports.Server._mcpIdentityWrapped = true;
|
|
72
|
+
}
|
|
73
|
+
// Wrap McpServer constructor if it exists
|
|
74
|
+
if (exports.McpServer && !exports.McpServer._mcpIdentityWrapped) {
|
|
75
|
+
const OriginalMcpServer = exports.McpServer;
|
|
76
|
+
exports.McpServer = class extends OriginalMcpServer {
|
|
77
|
+
constructor(...args) {
|
|
78
|
+
super(...args);
|
|
79
|
+
// Enhance after construction
|
|
80
|
+
setImmediate(async () => {
|
|
81
|
+
await (0, auto_enhance_1.enhanceMCPServer)(this);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
exports.McpServer._mcpIdentityWrapped = true;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
return exports;
|
|
89
|
+
};
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Hook into ES module imports
|
|
93
|
+
*/
|
|
94
|
+
async function hookESModules() {
|
|
95
|
+
// This requires Node.js experimental loader API
|
|
96
|
+
// Users would need to run with: node --loader @kya-os/mcp-i/loader
|
|
97
|
+
// For now, we'll use a different approach - monkey-patching the global
|
|
98
|
+
if (typeof globalThis !== 'undefined') {
|
|
99
|
+
// Store original import
|
|
100
|
+
const globalAny = globalThis;
|
|
101
|
+
const originalImport = globalAny.import || (async (id) => Promise.resolve(`${id}`).then(s => __importStar(require(s))));
|
|
102
|
+
// Override dynamic import
|
|
103
|
+
globalAny.import = async function (id) {
|
|
104
|
+
const module = await originalImport(id);
|
|
105
|
+
// Check if this is an MCP SDK module
|
|
106
|
+
if (id === '@modelcontextprotocol/sdk' ||
|
|
107
|
+
id === '@modelcontextprotocol/sdk/server' ||
|
|
108
|
+
id.includes('@modelcontextprotocol/sdk')) {
|
|
109
|
+
// Wrap constructors
|
|
110
|
+
if (module.Server && !module.Server._mcpIdentityWrapped) {
|
|
111
|
+
const OriginalServer = module.Server;
|
|
112
|
+
module.Server = class extends OriginalServer {
|
|
113
|
+
constructor(...args) {
|
|
114
|
+
super(...args);
|
|
115
|
+
setImmediate(async () => {
|
|
116
|
+
await (0, auto_enhance_1.enhanceMCPServer)(this);
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
module.Server._mcpIdentityWrapped = true;
|
|
121
|
+
}
|
|
122
|
+
if (module.McpServer && !module.McpServer._mcpIdentityWrapped) {
|
|
123
|
+
const OriginalMcpServer = module.McpServer;
|
|
124
|
+
module.McpServer = class extends OriginalMcpServer {
|
|
125
|
+
constructor(...args) {
|
|
126
|
+
super(...args);
|
|
127
|
+
setImmediate(async () => {
|
|
128
|
+
await (0, auto_enhance_1.enhanceMCPServer)(this);
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
module.McpServer._mcpIdentityWrapped = true;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return module;
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Initialize auto-enhancement
|
|
141
|
+
*/
|
|
142
|
+
async function initialize() {
|
|
143
|
+
if (isInitialized)
|
|
144
|
+
return;
|
|
145
|
+
isInitialized = true;
|
|
146
|
+
// Initialize identity immediately
|
|
147
|
+
(0, auto_enhance_1.initializeIdentity)().catch(err => {
|
|
148
|
+
console.error('[MCP-I] Failed to initialize identity:', err.message);
|
|
149
|
+
});
|
|
150
|
+
// Hook into module loading systems
|
|
151
|
+
try {
|
|
152
|
+
hookRequire();
|
|
153
|
+
}
|
|
154
|
+
catch (err) {
|
|
155
|
+
// CommonJS might not be available
|
|
156
|
+
}
|
|
157
|
+
try {
|
|
158
|
+
await hookESModules();
|
|
159
|
+
}
|
|
160
|
+
catch (err) {
|
|
161
|
+
// ES modules hooks might fail
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
// Auto-initialize when this module is imported
|
|
165
|
+
initialize();
|
|
166
|
+
//# sourceMappingURL=auto-init.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auto-init.js","sourceRoot":"","sources":["../src/auto-init.ts"],"names":[],"mappings":";AAAA;;;;;GAKG;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+IoB,2CAAqB;AA7I5C,iDAAsE;AAEtE,uDAAuD;AACvD,IAAI,aAAa,GAAG,KAAK,CAAC;AAE1B;;GAEG;AACH,SAAS,WAAW;IAClB,MAAM,MAAM,GAAG,OAAO,CAAC,QAAQ,CAAC,CAAC;IACjC,MAAM,eAAe,GAAG,MAAM,CAAC,SAAS,CAAC,OAAO,CAAC;IAEjD,MAAM,CAAC,SAAS,CAAC,OAAO,GAAG,UAAS,EAAU;QAC5C,MAAM,OAAO,GAAG,eAAe,CAAC,KAAK,CAAC,IAAI,EAAE,SAAS,CAAC,CAAC;QAEvD,qCAAqC;QACrC,IAAI,EAAE,KAAK,2BAA2B;YAClC,EAAE,KAAK,kCAAkC;YACzC,EAAE,KAAK,2CAA2C;YAClD,EAAE,KAAK,yCAAyC,EAAE,CAAC;YAErD,uCAAuC;YACvC,IAAI,OAAO,CAAC,MAAM,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,mBAAmB,EAAE,CAAC;gBAC1D,MAAM,cAAc,GAAG,OAAO,CAAC,MAAM,CAAC;gBACtC,OAAO,CAAC,MAAM,GAAG,KAAM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ED;;GAEG;AACH,KAAK,UAAU,UAAU;IACvB,IAAI,aAAa;QAAE,OAAO;IAC1B,aAAa,GAAG,IAAI,CAAC;IAErB,kCAAkC;IAClC,IAAA,iCAAkB,GAAE,CAAC,KAAK,CAAC,GAAG,CAAC,EAAE;QAC/B,OAAO,CAAC,KAAK,CAAC,wCAAwC,EAAE,GAAG,CAAC,OAAO,CAAC,CAAC;IACvE,CAAC,CAAC,CAAC;IAEH,mCAAmC;IACnC,IAAI,CAAC;QACH,WAAW,EAAE,CAAC;IAChB,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,kCAAkC;IACpC,CAAC;IAED,IAAI,CAAC;QACH,MAAM,aAAa,EAAE,CAAC;IACxB,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,8BAA8B;IAChC,CAAC;AACH,CAAC;AAED,+CAA+C;AAC/C,UAAU,EAAE,CAAC"}
|
package/dist/auto.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Auto-initialization for MCP Identity
|
|
3
|
+
*
|
|
4
|
+
* Just import this file to automatically enable MCP-I for any MCP server:
|
|
5
|
+
*
|
|
6
|
+
* ```typescript
|
|
7
|
+
* import "@kya-os/mcp-i/auto";
|
|
8
|
+
* ```
|
|
9
|
+
*
|
|
10
|
+
* That's it! Your MCP server now has identity.
|
|
11
|
+
*/
|
|
12
|
+
export {};
|
|
13
|
+
//# sourceMappingURL=auto.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auto.d.ts","sourceRoot":"","sources":["../src/auto.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;GAUG"}
|
package/dist/auto.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Auto-initialization for MCP Identity
|
|
4
|
+
*
|
|
5
|
+
* Just import this file to automatically enable MCP-I for any MCP server:
|
|
6
|
+
*
|
|
7
|
+
* ```typescript
|
|
8
|
+
* import "@kya-os/mcp-i/auto";
|
|
9
|
+
* ```
|
|
10
|
+
*
|
|
11
|
+
* That's it! Your MCP server now has identity.
|
|
12
|
+
*/
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
const index_1 = require("./index");
|
|
15
|
+
// Auto-initialize on import
|
|
16
|
+
(async () => {
|
|
17
|
+
try {
|
|
18
|
+
await (0, index_1.enableMCPIdentity)();
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
console.error('[MCP-I] Failed to auto-initialize:', error);
|
|
22
|
+
}
|
|
23
|
+
})();
|
|
24
|
+
//# sourceMappingURL=auto.js.map
|
package/dist/auto.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"auto.js","sourceRoot":"","sources":["../src/auto.ts"],"names":[],"mappings":";AAAA;;;;;;;;;;GAUG;;AAEH,mCAA4C;AAE5C,4BAA4B;AAC5B,CAAC,KAAK,IAAI,EAAE;IACV,IAAI,CAAC;QACH,MAAM,IAAA,yBAAiB,GAAE,CAAC;IAC5B,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO,CAAC,KAAK,CAAC,oCAAoC,EAAE,KAAK,CAAC,CAAC;IAC7D,CAAC;AACH,CAAC,CAAC,EAAE,CAAC"}
|
package/dist/crypto.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cryptographic utilities for MCP-I
|
|
3
|
+
* Implements Ed25519 signing and verification for challenge-response authentication
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Generate a new Ed25519 key pair
|
|
7
|
+
*/
|
|
8
|
+
export declare function generateKeyPair(): Promise<{
|
|
9
|
+
publicKey: string;
|
|
10
|
+
privateKey: string;
|
|
11
|
+
}>;
|
|
12
|
+
/**
|
|
13
|
+
* Sign a message with Ed25519
|
|
14
|
+
*/
|
|
15
|
+
export declare function sign(message: string | Buffer, privateKeyBase64: string): Promise<string>;
|
|
16
|
+
/**
|
|
17
|
+
* Verify an Ed25519 signature
|
|
18
|
+
*/
|
|
19
|
+
export declare function verify(message: string | Buffer, signatureBase64: string, publicKeyBase64: string): Promise<boolean>;
|
|
20
|
+
/**
|
|
21
|
+
* Generate a cryptographically secure nonce
|
|
22
|
+
*/
|
|
23
|
+
export declare function generateNonce(length?: number): string;
|
|
24
|
+
/**
|
|
25
|
+
* Constant-time string comparison to prevent timing attacks
|
|
26
|
+
*/
|
|
27
|
+
export declare function constantTimeEqual(a: string, b: string): boolean;
|
|
28
|
+
/**
|
|
29
|
+
* Convert Ed25519 public key to did:key format
|
|
30
|
+
*/
|
|
31
|
+
export declare function publicKeyToDid(publicKeyBase64: string): string;
|
|
32
|
+
//# sourceMappingURL=crypto.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crypto.d.ts","sourceRoot":"","sources":["../src/crypto.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAKH;;GAEG;AACH,wBAAsB,eAAe,IAAI,OAAO,CAAC;IAC/C,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,EAAE,MAAM,CAAC;CACpB,CAAC,CAQD;AAED;;GAEG;AACH,wBAAsB,IAAI,CACxB,OAAO,EAAE,MAAM,GAAG,MAAM,EACxB,gBAAgB,EAAE,MAAM,GACvB,OAAO,CAAC,MAAM,CAAC,CASjB;AAED;;GAEG;AACH,wBAAsB,MAAM,CAC1B,OAAO,EAAE,MAAM,GAAG,MAAM,EACxB,eAAe,EAAE,MAAM,EACvB,eAAe,EAAE,MAAM,GACtB,OAAO,CAAC,OAAO,CAAC,CAalB;AAED;;GAEG;AACH,wBAAgB,aAAa,CAAC,MAAM,GAAE,MAAW,GAAG,MAAM,CAEzD;AAED;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,MAAM,GAAG,OAAO,CAW/D;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,eAAe,EAAE,MAAM,GAAG,MAAM,CAS9D"}
|
package/dist/crypto.js
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Cryptographic utilities for MCP-I
|
|
4
|
+
* Implements Ed25519 signing and verification for challenge-response authentication
|
|
5
|
+
*/
|
|
6
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
7
|
+
if (k2 === undefined) k2 = k;
|
|
8
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
9
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
10
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
11
|
+
}
|
|
12
|
+
Object.defineProperty(o, k2, desc);
|
|
13
|
+
}) : (function(o, m, k, k2) {
|
|
14
|
+
if (k2 === undefined) k2 = k;
|
|
15
|
+
o[k2] = m[k];
|
|
16
|
+
}));
|
|
17
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
18
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
19
|
+
}) : function(o, v) {
|
|
20
|
+
o["default"] = v;
|
|
21
|
+
});
|
|
22
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
23
|
+
var ownKeys = function(o) {
|
|
24
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
25
|
+
var ar = [];
|
|
26
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
27
|
+
return ar;
|
|
28
|
+
};
|
|
29
|
+
return ownKeys(o);
|
|
30
|
+
};
|
|
31
|
+
return function (mod) {
|
|
32
|
+
if (mod && mod.__esModule) return mod;
|
|
33
|
+
var result = {};
|
|
34
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
35
|
+
__setModuleDefault(result, mod);
|
|
36
|
+
return result;
|
|
37
|
+
};
|
|
38
|
+
})();
|
|
39
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
40
|
+
exports.generateKeyPair = generateKeyPair;
|
|
41
|
+
exports.sign = sign;
|
|
42
|
+
exports.verify = verify;
|
|
43
|
+
exports.generateNonce = generateNonce;
|
|
44
|
+
exports.constantTimeEqual = constantTimeEqual;
|
|
45
|
+
exports.publicKeyToDid = publicKeyToDid;
|
|
46
|
+
const ed25519 = __importStar(require("@noble/ed25519"));
|
|
47
|
+
const crypto_1 = require("crypto");
|
|
48
|
+
/**
|
|
49
|
+
* Generate a new Ed25519 key pair
|
|
50
|
+
*/
|
|
51
|
+
async function generateKeyPair() {
|
|
52
|
+
const privateKey = ed25519.utils.randomPrivateKey();
|
|
53
|
+
const publicKey = await ed25519.getPublicKeyAsync(privateKey);
|
|
54
|
+
return {
|
|
55
|
+
publicKey: Buffer.from(publicKey).toString('base64'),
|
|
56
|
+
privateKey: Buffer.from(privateKey).toString('base64')
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Sign a message with Ed25519
|
|
61
|
+
*/
|
|
62
|
+
async function sign(message, privateKeyBase64) {
|
|
63
|
+
const messageBuffer = typeof message === 'string'
|
|
64
|
+
? Buffer.from(message, 'utf-8')
|
|
65
|
+
: message;
|
|
66
|
+
const privateKey = Buffer.from(privateKeyBase64, 'base64');
|
|
67
|
+
const signature = await ed25519.signAsync(messageBuffer, privateKey);
|
|
68
|
+
return Buffer.from(signature).toString('base64');
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Verify an Ed25519 signature
|
|
72
|
+
*/
|
|
73
|
+
async function verify(message, signatureBase64, publicKeyBase64) {
|
|
74
|
+
try {
|
|
75
|
+
const messageBuffer = typeof message === 'string'
|
|
76
|
+
? Buffer.from(message, 'utf-8')
|
|
77
|
+
: message;
|
|
78
|
+
const signature = Buffer.from(signatureBase64, 'base64');
|
|
79
|
+
const publicKey = Buffer.from(publicKeyBase64, 'base64');
|
|
80
|
+
return await ed25519.verifyAsync(signature, messageBuffer, publicKey);
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
return false;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Generate a cryptographically secure nonce
|
|
88
|
+
*/
|
|
89
|
+
function generateNonce(length = 32) {
|
|
90
|
+
return (0, crypto_1.randomBytes)(length).toString('hex');
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Constant-time string comparison to prevent timing attacks
|
|
94
|
+
*/
|
|
95
|
+
function constantTimeEqual(a, b) {
|
|
96
|
+
if (a.length !== b.length) {
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
let result = 0;
|
|
100
|
+
for (let i = 0; i < a.length; i++) {
|
|
101
|
+
result |= a.charCodeAt(i) ^ b.charCodeAt(i);
|
|
102
|
+
}
|
|
103
|
+
return result === 0;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Convert Ed25519 public key to did:key format
|
|
107
|
+
*/
|
|
108
|
+
function publicKeyToDid(publicKeyBase64) {
|
|
109
|
+
const publicKey = Buffer.from(publicKeyBase64, 'base64');
|
|
110
|
+
// Multicodec ed25519-pub header (0xed 0x01)
|
|
111
|
+
const multicodec = Buffer.from([0xed, 0x01]);
|
|
112
|
+
const multikey = Buffer.concat([multicodec, publicKey]);
|
|
113
|
+
// Base58 encode (simplified - in production use a proper base58 library)
|
|
114
|
+
// For now, just return a placeholder
|
|
115
|
+
return `did:key:z${multikey.toString('base64').replace(/\+/g, '-').replace(/\//g, '_').replace(/=/g, '')}`;
|
|
116
|
+
}
|
|
117
|
+
//# sourceMappingURL=crypto.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"crypto.js","sourceRoot":"","sources":["../src/crypto.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;;;;;;;;;;;;;;;;;;;;;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