@griffinbank/mcp-server
Version:
Griffin API MCP Server - Model Context Protocol server for Griffin API
142 lines (141 loc) • 5.87 kB
JavaScript
import fetch from "node-fetch";
async function request(baseUrl, apiKey, endpoint, method = "GET", body) {
const url = `${baseUrl}${endpoint}`;
const options = {
method,
headers: {
"Authorization": `GriffinAPIKey ${apiKey}`,
"Content-Type": "application/json"
}
};
if (body && (method === "POST" || method === "PUT" || method === "PATCH")) {
options.body = JSON.stringify(body);
}
const response = await fetch(url, options);
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Griffin API error (${response.status}): ${errorText}`);
}
return response.json();
}
export class GriffinAPIClient {
baseUrl;
apiKey;
organizationUrl = null;
constructor(griffinAPIBaseURL, griffinAPIKey) {
this.baseUrl = griffinAPIBaseURL;
this.apiKey = griffinAPIKey;
}
async getOrganizationUrl() {
if (!this.organizationUrl) {
const index = await request(this.baseUrl, this.apiKey, "/v0/index");
this.organizationUrl = index["organization-url"];
}
return this.organizationUrl;
}
async getIndex() {
return request(this.baseUrl, this.apiKey, "/v0/index");
}
async getBankAccount(accountUrl) {
return request(this.baseUrl, this.apiKey, accountUrl);
}
async getLegalPerson(legalPersonUrl) {
return request(this.baseUrl, this.apiKey, legalPersonUrl);
}
async getPayment(paymentUrl) {
return request(this.baseUrl, this.apiKey, paymentUrl);
}
async getPayee(payeeUrl) {
return request(this.baseUrl, this.apiKey, payeeUrl);
}
async listTransactions(accountUrl, limit = 10) {
return request(this.baseUrl, this.apiKey, `${accountUrl}/transactions?page[size]=${limit}`);
}
async listBankAccounts(filters = {}) {
const organizationUrl = await this.getOrganizationUrl();
let endpoint = `${organizationUrl}/bank/accounts?include[]=beneficiary&include[]=owner`;
if (filters.filterStatus) {
endpoint += `&filter[account-status][in][]=${filters.filterStatus}`;
}
if (filters.filterBankProductType) {
endpoint += `&filter[bank-product-type][in][]=${filters.filterBankProductType}`;
}
if (filters.filterPooledFunds !== undefined) {
endpoint += `&filter[pooled-funds][eq]=${filters.filterPooledFunds}`;
}
if (filters.beneficiaryUrl) {
endpoint += `&filter[beneficiary-url][eq]=${encodeURIComponent(filters.beneficiaryUrl)}`;
}
if (filters.ownerUrl) {
endpoint += `&filter[owner-url][eq]=${encodeURIComponent(filters.ownerUrl)}`;
}
return request(this.baseUrl, this.apiKey, endpoint);
}
async listLegalPersons(filters = {}) {
const organizationUrl = await this.getOrganizationUrl();
let endpoint = `${organizationUrl}/legal-persons`;
endpoint += "?include[]=latest-verification";
endpoint += "&include[]=latest-risk-rating";
if (filters.sort) {
endpoint += `&sort=${filters.sort}`;
}
else {
endpoint += `&sort=-created-at`;
}
if (filters.filterApplicationStatus) {
endpoint += `&filter[application-status][eq]=${filters.filterApplicationStatus}`;
}
return request(this.baseUrl, this.apiKey, endpoint);
}
async listPayments(filters = {}) {
const organizationUrl = await this.getOrganizationUrl();
let endpoint = `${organizationUrl}/payments`;
const queryParams = [];
queryParams.push(`sort=${filters.sort || "-created-at"}`);
if (filters.filterPaymentDirection) {
queryParams.push(`filter[payment-direction][eq]=${filters.filterPaymentDirection}`);
}
if (filters.filterRejected !== undefined) {
queryParams.push(`filter[rejected][eq]=${filters.filterRejected}`);
}
if (filters.filterCreatedAfter) {
queryParams.push(`filter[created-at][gt]=${encodeURIComponent(filters.filterCreatedAfter)}`);
}
if (filters.filterCreatedBefore) {
queryParams.push(`filter[created-at][lt]=${encodeURIComponent(filters.filterCreatedBefore)}`);
}
queryParams.push("include[]=bank-account");
queryParams.push("include[]=latest-submission");
queryParams.push("include[]=rejected-by");
queryParams.push("include[]=created-by");
endpoint += `?${queryParams.join("&")}`;
return request(this.baseUrl, this.apiKey, endpoint);
}
async listPayees(legalPersonUrl) {
const endpoint = `${legalPersonUrl}/bank/payees?include[]=cop-requests`;
return request(this.baseUrl, this.apiKey, endpoint);
}
async createPayment(sourceAccountUrl, paymentData) {
const endpoint = `${sourceAccountUrl}/payments`;
return request(this.baseUrl, this.apiKey, endpoint, "POST", paymentData);
}
async submitPayment(paymentUrl, paymentScheme) {
const endpoint = `${paymentUrl}/submissions`;
const submissionData = {
"payment-scheme": paymentScheme
};
return request(this.baseUrl, this.apiKey, endpoint, "POST", submissionData);
}
async openAccount(displayName, bankProductType) {
const organizationUrl = await this.getOrganizationUrl();
const endpoint = `${organizationUrl}/bank/accounts`;
const requestBody = {
"display-name": displayName,
"bank-product-type": bankProductType
};
return await request(this.baseUrl, this.apiKey, endpoint, "POST", requestBody);
}
async getApiKey(apiKeyUrl) {
return request(this.baseUrl, this.apiKey, apiKeyUrl);
}
}