-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathindex.ts
More file actions
162 lines (141 loc) · 4.77 KB
/
Copy pathindex.ts
File metadata and controls
162 lines (141 loc) · 4.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { defaultDevelopersApi } from '@/shared/api';
import { tools } from '@/shared/tools';
import { ToolContext } from '@/shared/types';
import { version } from '../../package.json';
export type { DevelopersApi, Tool, ToolContext } from '@/shared/types';
export { tools } from '@/shared/tools';
export interface MastercardDevelopersAgentToolkitConfig {
service?: string;
apiSpecification?: string;
}
export class MastercardDevelopersAgentToolkit extends McpServer {
constructor(config: MastercardDevelopersAgentToolkitConfig = {}) {
super({
name: 'mastercard-developers-mcp',
version: version,
});
this.registerAllTools(config);
}
private registerAllTools(config: MastercardDevelopersAgentToolkitConfig) {
const context = buildContext(config);
const availableTools = tools(context);
const enabledTools = availableTools.filter((tool) => {
// If serviceId is provided, disable the services list tool
if (context.serviceId && tool.name === 'get-services-list') {
return false;
}
return true;
});
enabledTools.forEach((tool) => {
this.registerTool(
tool.name,
{
title: tool.title,
description: tool.description,
inputSchema: tool.parameters,
annotations: tool.annotations,
},
async (params: any) => {
try {
const result = await tool.execute(params);
return { content: [{ type: 'text' as const, text: result }] };
} catch (error) {
const message =
error instanceof Error ? error.message : String(error);
return {
content: [{ type: 'text' as const, text: message }],
isError: true,
};
}
}
);
});
}
}
export function buildContext(
config: MastercardDevelopersAgentToolkitConfig
): ToolContext {
const context: ToolContext = { client: defaultDevelopersApi };
if (config.service != null) {
const serviceId = parseServiceIdFromUrl(config.service);
if (serviceId == null) {
throw new Error(
'Invalid service URL provided. It should be in the format: https://developer.mastercard.com/<service-id>/documentation/**'
);
}
context.serviceId = serviceId;
} else if (config.apiSpecification != null) {
const parsed = parseAPISpecificationPathAndServiceId(
config.apiSpecification
);
if (parsed?.serviceId == null || parsed?.apiSpecificationPath == null) {
throw new Error(
'Invalid API specification path provided. It should be in the format: https://static.developer.mastercard.com/content/<service-id>/swagger/<nested-file-path>.yaml'
);
}
context.serviceId = parsed.serviceId;
context.apiSpecificationPath = parsed.apiSpecificationPath;
}
return context;
}
function validateServiceId(serviceId: string): boolean {
return /^[a-z]+(?:-[a-z0-9]+)*$/i.test(serviceId);
}
function parseServiceIdFromUrl(input: string): string | null {
try {
// Extract from https://developer.mastercard.com/<service-id>/documentation/**
const url = new URL(input);
if (url.hostname !== 'developer.mastercard.com') {
return null;
}
const pathParts = url.pathname.split('/').filter((part) => part.length > 0);
// Path should be: /<service-id>/documentation/...
if (pathParts.length >= 2 && pathParts[1] === 'documentation') {
const serviceId = pathParts[0];
if (serviceId && validateServiceId(serviceId)) {
return serviceId.toLowerCase();
}
}
return null;
} catch {
// Not a valid URL, return null
return null;
}
}
function parseAPISpecificationPathAndServiceId(
input: string
): { serviceId: string; apiSpecificationPath: string } | null {
try {
// Try to parse as URL first
const url = new URL(input);
// Handle full URL: https://static.developer.mastercard.com/content/open-finance-us/swagger/openbanking-us.yaml
if (url.hostname !== 'static.developer.mastercard.com') {
return null;
}
const pathParts = url.pathname.split('/').filter((part) => part.length > 0);
// Path should be: content/<service-id>/swagger/<nested-file-path>.yaml
if (
pathParts.length >= 4 &&
pathParts[0] === 'content' &&
pathParts[2] === 'swagger'
) {
const serviceId = pathParts[1];
const file = pathParts.slice(3).join('/');
if (
serviceId &&
file &&
validateServiceId(serviceId) &&
file.endsWith('.yaml')
) {
return {
serviceId: serviceId.toLowerCase(),
apiSpecificationPath: `/${serviceId.toLowerCase()}/swagger/${file}`,
};
}
}
return null;
} catch {
return null;
}
}