MCPF TypeScript SDK
  ](https://www.npmjs.com/package/mcpf-typescript) 
MCPF的完整TypeScript SDK -所有MCPF信任框架组件的类型安全、现代接口:DID/VC、ANS、MCP注册表和A2A委托。
🌟 什么是MCPF字体?
MCPF TypeScript SDK为整个MCPF信任框架提供了一个完全类型安全的接口:
import { MCPF } from 'mcpf-typescript';
// Initialize SDK
const mcpf = new MCPF({
ansUrl: 'https://ans.veritrust.vc',
registryUrl: 'https://ans.veritrust.vc/mcp',
a2aUrl: 'https://a2a.example.com'
});
// Resolve agent name → verify credentials → check delegation
const agent = await mcpf.resolveAndVerify('fraud-detector.risk.bank.example.agent');
if (await mcpf.canDelegate(agent.did, targetDid, 'analyze')) {
const result = await executeTask(agent, targetDid, taskData);
}特性
- 🔐 DID/VC管理 -创建DID、颁发/验证凭据、管理吊销
- 📛 ANS客户端 -解析代理名称、注册代理、搜索目录
- 📋 MCP注册表 -发现受信任的MCP服务器,验证凭据
- 🔄 A2A代表团 -检查委派权限,管理策略
- ✅ 完全验证 -端到端凭据和策略验证
- ⚡ 全类型安全 -全面的TypeScript类型
- 🎯 现代ES模块 -ESM支持树木摇动
- 🧪 测试良好 -覆盖率超过90%的全面测试套件
- 📦 零依赖 -仅本机获取API
🚀 快速开始
安装
npm install mcpf-typescript
# or
yarn add mcpf-typescript
# or
pnpm add mcpf-typescript基本用法
import { MCPF } from 'mcpf-typescript';
const mcpf = new MCPF();
// Resolve an agent name
const agent = await mcpf.ans.resolve('fraud-detector.risk.bank.example.agent');
console.log(`Agent DID: ${agent.did}`);
console.log(`Capabilities: ${agent.capabilities.join(', ')}`);
// Verify agent credential
const isValid = await mcpf.did.verifyCredential(agent.credentialUrl);
console.log(`Credential valid: ${isValid}`);
// Check delegation permission
const canDelegate = await mcpf.a2a.checkDelegation({
fromDid: 'did:web:agent1.example',
toDid: 'did:web:agent2.example',
action: 'analyze'
});
console.log(`Delegation allowed: ${canDelegate.allowed}`);📖 api参考
MCPF客户端(统一接口)
import { MCPF, MCPFConfig } from 'mcpf-typescript';
const config: MCPFConfig = {
ansUrl: 'https://ans.veritrust.vc',
registryUrl: 'https://ans.veritrust.vc/mcp',
a2aUrl: 'https://a2a.example.com',
didResolverUrl: 'https://resolver.example.com'
};
const mcpf = new MCPF(config);
// Unified operations
const agent = await mcpf.resolveAndVerify(agentName);
const canDelegate = await mcpf.canDelegate(fromDid, toDid, action);
const server = await mcpf.findMcpServer({ capability: 'weather' });DID/VC模块
import { DIDManager, VCIssuer, VCVerifier } from 'mcpf-typescript';
// DID operations
const didManager = new DIDManager();
const did = didManager.createDid({ method: 'web', domain: 'example.com' });
const didDoc = await didManager.resolveDid(did);
// Issue credentials
const issuer = new VCIssuer({
issuerDid: 'did:web:veritrust.vc',
privateKey: privateKey
});
const credential = issuer.issueCredential({
subjectDid: 'did:web:agent.example',
credentialType: 'AgentOwnershipCredential',
claims: { permissions: ['query', 'analyze'] }
});
// Verify credentials
const verifier = new VCVerifier();
const result = await verifier.verifyCredential(credential);
console.log(`Valid: ${result.valid}, Revoked: ${result.isRevoked}`);ANS模块
import { ANSClient, AgentCard } from 'mcpf-typescript';
const ans = new ANSClient('https://ans.veritrust.vc');
// Resolve agent name
const agentCard: AgentCard = await ans.resolve({
name: 'fraud-detector.risk.bank.example.agent',
version: '1.0.0' // Optional
});
// Register agent
await ans.register({
name: 'my-agent.company.example.agent',
version: '1.0.0',
did: 'did:web:company.example:agent:my-agent',
provider: 'My Company',
capabilities: ['query', 'analyze'],
endpoints: {
agent: 'https://company.example/agent',
presentations: 'https://company.example/agent/vp'
}
});
// Search agents
const results = await ans.search({ capability: 'fraud-detection' });MCP注册表模块
import { MCPRegistry, MCPServer } from 'mcpf-typescript';
const registry = new MCPRegistry('https://ans.veritrust.vc/mcp');
// List MCP servers
const servers = await registry.listServers({ page: 1, limit: 50 });
// Get server by DID
const server: MCPServer = await registry.getServer(
'did:web:weather.example.com:mcp:api'
);
// Search by capability
const weatherServers = await registry.search({
capability: 'getCurrentWeather'
});
// Register MCP server
await registry.registerServer({
did: 'did:web:myapi.example.com:mcp',
endpoint: 'https://myapi.example.com/mcp',
manifest: 'https://myapi.example.com/mcp/manifest.json',
credentials: [...],
metadata: {
capabilities: ['query', 'analyze'],
organization: 'My Company',
country: 'US'
}
});A2A模块
import { A2ARegistry, DelegationResult, Policy } from 'mcpf-typescript';
const a2a = new A2ARegistry('https://a2a.example.com');
// Check delegation permission
const result: DelegationResult = await a2a.checkDelegation({
fromDid: 'did:web:fraud-detector.bank.example',
toDid: 'did:web:risk-analyzer.bank.example',
action: 'analyze'
});
if (result.allowed) {
console.log(`Delegation allowed: ${result.policy}`);
} else {
console.log(`Delegation denied: ${result.reason}`);
}
// Register delegation policy
const policy: Policy = await a2a.registerPolicy({
fromAgent: 'did:web:agent1.example',
toAgent: 'did:web:agent2.example',
allowedActions: ['query', 'analyze'],
constraints: {
maxDuration: 3600,
scope: ['transaction-data']
},
issuedBy: 'did:web:example.com',
validFrom: '2025-01-01T00:00:00Z',
validUntil: '2026-01-01T00:00:00Z'
});
// Get audit log
const audit = await a2a.getAuditLog({
fromDid: 'did:web:agent1.example',
startDate: '2025-01-01'
});📝 完整示例
示例1:端到端代理验证
import { MCPF } from 'mcpf-typescript';
async function verifyAgentChain() {
const mcpf = new MCPF({
ansUrl: 'https://ans.veritrust.vc',
registryUrl: 'https://ans.veritrust.vc/mcp'
});
// 1. Resolve agent name to DID
const agent = await mcpf.ans.resolve({
name: 'fraud-detector.risk.bank.example.agent'
});
console.log(`✓ Resolved: ${agent.name} → ${agent.did}`);
// 2. Get DID document
const didDoc = await mcpf.did.resolveDid(agent.did);
console.log(`✓ DID Document: ${didDoc.verificationMethod.length} keys`);
// 3. Verify agent credential
const verification = await mcpf.did.verifyCredentialUrl(agent.credentialUrl);
console.log(`✓ Credential valid: ${verification.valid}`);
console.log(`✓ Not revoked: ${!verification.isRevoked}`);
// 4. Check all verifications passed
if (verification.valid && !verification.isRevoked) {
console.log('✅ Agent fully verified!');
return agent;
} else {
console.log('❌ Agent verification failed');
return null;
}
}
verifyAgentChain();示例2:委派工作流
import { MCPF } from 'mcpf-typescript';
async function delegationWorkflow() {
const mcpf = new MCPF({
ansUrl: 'https://ans.veritrust.vc',
a2aUrl: 'https://a2a.example.com'
});
// Resolve both agents
const fromAgent = await mcpf.ans.resolve({
name: 'fraud-detector.risk.bank.example.agent'
});
const toAgent = await mcpf.ans.resolve({
name: 'risk-analyzer.analytics.bank.example.agent'
});
// Verify both agents
const fromValid = await mcpf.did.verifyAgent(fromAgent.did);
const toValid = await mcpf.did.verifyAgent(toAgent.did);
if (!fromValid || !toValid) {
console.log('❌ Agent verification failed');
return;
}
// Check delegation permission
const delegation = await mcpf.a2a.checkDelegation({
fromDid: fromAgent.did,
toDid: toAgent.did,
action: 'analyze'
});
if (delegation.allowed) {
console.log('✅ Delegation allowed');
console.log(` Policy: ${delegation.policy?.id}`);
console.log(` Constraints: ${JSON.stringify(delegation.policy?.constraints)}`);
// Execute delegation
const result = await executeAnalysis(fromAgent, toAgent, data);
console.log(`✅ Task completed: ${result}`);
} else {
console.log(`❌ Delegation denied: ${delegation.reason}`);
}
}
delegationWorkflow();示例3:MCP服务器发现
import { MCPF } from 'mcpf-typescript';
async function findWeatherServer() {
const mcpf = new MCPF({
registryUrl: 'https://ans.veritrust.vc/mcp'
});
// Search for weather servers
const servers = await mcpf.registry.search({
capability: 'getCurrentWeather',
country: 'US'
});
console.log(`Found ${servers.items.length} weather servers:`);
for (const server of servers.items) {
console.log(`\n Server: ${server.did}`);
console.log(` Endpoint: ${server.endpoint}`);
console.log(` Organization: ${server.metadata.organization}`);
console.log(` Capabilities: ${server.metadata.capabilities.join(', ')}`);
// Verify server credential
if (server.credentials.length > 0) {
const cred = server.credentials[0];
const verification = await mcpf.did.verifyCredentialUrl(cred.credentialUrl);
console.log(` Verified: ${verification.valid ? '✓' : '✗'}`);
}
}
}
findWeatherServer();示例4:完全集成
import { MCPF } from 'mcpf-typescript';
async function completeWorkflow() {
/**
* Complete MCPF workflow: resolve → verify → check delegation → execute
*/
const mcpf = new MCPF({
ansUrl: 'https://ans.veritrust.vc',
registryUrl: 'https://ans.veritrust.vc/mcp',
a2aUrl: 'https://a2a.example.com'
});
// 1. Resolve agent names
console.log('1️⃣ Resolving agent names...');
const fromAgent = await mcpf.ans.resolve({
name: 'fraud-detector.risk.bank.example.agent'
});
const toAgent = await mcpf.ans.resolve({
name: 'risk-analyzer.analytics.bank.example.agent'
});
// 2. Verify credentials
console.log('2️⃣ Verifying credentials...');
const fromValid = await mcpf.did.verifyAgent(fromAgent.did);
const toValid = await mcpf.did.verifyAgent(toAgent.did);
if (!fromValid || !toValid) {
console.log('❌ Credential verification failed');
return;
}
// 3. Check if MCP server is registered
console.log('3️⃣ Checking MCP registry...');
try {
const mcpServer = await mcpf.registry.getServer(toAgent.did);
console.log(` ✓ MCP server registered: ${mcpServer.endpoint}`);
} catch {
console.log(' ℹ️ Not an MCP server');
}
// 4. Check delegation permission
console.log('4️⃣ Checking delegation permission...');
const delegation = await mcpf.a2a.checkDelegation({
fromDid: fromAgent.did,
toDid: toAgent.did,
action: 'analyze'
});
if (!delegation.allowed) {
console.log(`❌ Delegation denied: ${delegation.reason}`);
return;
}
console.log(` ✓ Delegation allowed (policy: ${delegation.policy?.id})`);
// 5. Execute task
console.log('5️⃣ Executing task...');
const result = {
fromAgent: fromAgent.name,
toAgent: toAgent.name,
action: 'analyze',
status: 'success'
};
console.log('✅ Workflow complete!');
return result;
}
completeWorkflow();🧪 测试
# Run tests
npm test
# With coverage
npm run test:coverage
# Type checking
npm run type-check
# Linting
npm run lint
# Build
npm run build📚 文档
完整文档可在https://mcpf.dev/docs/typescript
🔧 配置
环境变量
# ANS endpoint
MCPF_ANS_URL=https://ans.veritrust.vc
# MCP Registry endpoint
MCPF_REGISTRY_URL=https://ans.veritrust.vc/mcp
# A2A Registry endpoint
MCPF_A2A_URL=https://a2a.example.com
# DID Resolver
MCPF_DID_RESOLVER_URL=https://resolver.example.com
# Timeout (ms)
MCPF_TIMEOUT=30000程序化配置
import { MCPF, MCPFConfig } from 'mcpf-typescript';
const config: MCPFConfig = {
ansUrl: process.env.MCPF_ANS_URL || 'https://ans.veritrust.vc',
registryUrl: process.env.MCPF_REGISTRY_URL || 'https://ans.veritrust.vc/mcp',
a2aUrl: process.env.MCPF_A2A_URL,
didResolverUrl: process.env.MCPF_DID_RESOLVER_URL,
timeout: 30000,
verifySsl: true
};
const mcpf = new MCPF(config);🤝 贡献
看 贡献.md 作为指导方针。
📝 许可证
MIT许可证-请参阅 许可证
📞 联系
- 网站: https://mcpf.dev
- github: https://github.com/MCPTrustFramework/MCPF-typescript
- 问题: https://github.com/MCPTrustFramework/MCPF-typescript/issues
- npm: https://www.npmjs.com/package/mcpf-typescript
🔗 相关项目
- MCPF规范 -SSOT
- MCPF做了vc -DID/VC基础设施
- MCPF安 -代理名称服务
- MCPF注册表 -MCP信托登记处
- MCPF-a2a注册表 -A2A代表团
- MCPF python -Python SDK
______________________________________________________________________
版本: 1.0.0-alpha\ 最后更新时间: 2025年12月31日\ 状态: 生产准备就绪
