@kya-os/mcp-i
Version:
The TypeScript MCP framework with identity features built-in
176 lines (175 loc) • 6.54 kB
JavaScript
;
/**
* Example usage of XMCP-I test infrastructure
*
* This file demonstrates how to use the test infrastructure for testing
* XMCP-I applications without hitting external services.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.setupTestEnvironment = setupTestEnvironment;
exports.exampleMockIdentityProvider = exampleMockIdentityProvider;
exports.exampleProofTesting = exampleProofTesting;
exports.exampleKTAInterception = exampleKTAInterception;
exports.exampleRuntimeIntegration = exampleRuntimeIntegration;
exports.exampleCompleteTestScenario = exampleCompleteTestScenario;
const index_js_1 = require("../index.js");
const test_1 = require("@kya-os/contracts/test");
/**
* Example: Setting up test environment
*/
function setupTestEnvironment() {
// Configure test environment with custom seed
(0, index_js_1.configureTestEnvironment)({
seed: "my-test-suite",
skipKTACalls: true,
});
// Verify we're in test mode
console.log("Test mode:", (0, index_js_1.isTestMode)());
// Validate test environment
const validation = (0, index_js_1.validateTestEnvironment)();
if (!validation.valid) {
throw new Error(`Test environment validation failed: ${validation.errors.join(", ")}`);
}
if (validation.warnings.length > 0) {
console.warn("Test environment warnings:", validation.warnings);
}
}
/**
* Example: Using mock identity provider
*/
function exampleMockIdentityProvider() {
const mockProvider = new index_js_1.MockIdentityProvider();
// Generate a custom test identity
const customIdentity = (0, index_js_1.generateTestIdentity)("my-custom-test", {
did: "did:test:my-custom-agent",
kid: "key-custom-1",
});
// Set the identity in the provider
mockProvider.setIdentity("custom", customIdentity);
// Set delegation status
mockProvider.setDelegation(`${customIdentity.did}:${customIdentity.kid}`, "active");
// Simulate KTA failures for testing error scenarios
mockProvider.simulateKTAFailure("network");
return mockProvider;
}
/**
* Example: Testing proof generation and verification
*/
async function exampleProofTesting() {
// Create mock request and response data
const mockRequest = {
method: "tools/call",
params: {
name: "greet",
arguments: { name: "Alice" },
},
};
const mockResponse = {
content: [
{
type: "text",
text: "Hello, Alice!",
},
],
};
// Create a mock proof
const mockProof = (0, index_js_1.createMockProof)({
did: test_1.TEST_DIDS.AGENT_1,
kid: test_1.TEST_KEY_IDS.KEY_TEST_1,
request: mockRequest,
response: mockResponse,
sessionId: "sess_test_example",
nonce: "example_nonce",
audience: "example.com",
});
// Verify the proof locally
const verificationResult = await (0, index_js_1.verifyProofLocally)(mockProof, mockRequest, mockResponse);
console.log("Proof verification result:", {
valid: verificationResult.valid,
did: verificationResult.did,
kid: verificationResult.kid,
signatureValid: verificationResult.signature.valid,
proofValid: verificationResult.proof.valid,
sessionValid: verificationResult.session.valid,
errors: verificationResult.errors,
warnings: verificationResult.warnings,
});
return verificationResult;
}
/**
* Example: Intercepting KTA calls
*/
async function exampleKTAInterception() {
// Mock a KTA registration call
const registrationResult = await (0, index_js_1.interceptKTACall)("register", async () => {
// This would normally make a real KTA call
throw new Error("Should not reach here in test mode");
}, async () => {
// Custom mock response
return {
success: true,
agentURL: "https://custom-test-kta.example.com/agents/my-agent",
agentId: "my-agent",
agentSlug: "my-agent",
verificationEndpoint: "https://custom-test-kta.example.com/verify/my-agent",
conformanceCapabilities: ["handshake", "signing", "verification"],
mirrorStatus: "pending",
};
});
console.log("Mock registration result:", registrationResult);
// Mock a delegation check
const delegationResult = await (0, index_js_1.interceptKTACall)("checkDelegation", async () => {
throw new Error("Should not reach here in test mode");
});
console.log("Mock delegation result:", delegationResult);
return { registrationResult, delegationResult };
}
/**
* Example: Using test runtime integration
*/
function exampleRuntimeIntegration() {
// Get test identity for runtime use
const testIdentity = (0, index_js_1.getTestIdentityForRuntime)("agent1");
console.log("Test identity for runtime:", {
did: testIdentity.did,
kid: testIdentity.kid,
// Don't log private key in real usage
});
// Create test session context
const sessionContext = (0, index_js_1.createTestSessionContext)({
sessionId: "sess_example_123",
audience: "my-app.example.com",
nonce: "example_nonce_456",
});
console.log("Test session context:", sessionContext);
return { testIdentity, sessionContext };
}
/**
* Example: Complete test scenario
*/
async function exampleCompleteTestScenario() {
console.log("=== XMCP-I Test Infrastructure Example ===");
// 1. Setup test environment
console.log("\n1. Setting up test environment...");
setupTestEnvironment();
// 2. Configure mock identity provider
console.log("\n2. Configuring mock identity provider...");
const mockProvider = exampleMockIdentityProvider();
// 3. Test proof generation and verification
console.log("\n3. Testing proof generation and verification...");
const proofResult = await exampleProofTesting();
// 4. Test KTA call interception
console.log("\n4. Testing KTA call interception...");
const ktaResults = await exampleKTAInterception();
// 5. Test runtime integration
console.log("\n5. Testing runtime integration...");
const runtimeResults = exampleRuntimeIntegration();
console.log("\n=== Test Infrastructure Example Complete ===");
return {
mockProvider,
proofResult,
ktaResults,
runtimeResults,
};
}
// Functions are already exported above