moa-mcp-server
Version:
MCP Server for Memory of Agents (MOA) API - Memory Management & Intelligent Memory endpoints only
224 lines (223 loc) • 7.58 kB
JavaScript
import axios from 'axios';
import { MOAAPIError } from '../types/index.js';
export class MOAAPIService {
client;
config;
constructor(config) {
this.config = config;
this.client = axios.create({
baseURL: config.baseUrl,
timeout: config.timeout,
headers: {
'Authorization': `Bearer ${config.apiKey}`,
'Content-Type': 'application/json',
'User-Agent': 'moa-mcp-server/0.1.2',
},
});
this.client.interceptors.response.use((response) => response, (error) => {
if (error.response) {
throw new MOAAPIError(error.response.data?.detail || error.response.data?.message || error.message, error.response.status, error.response.data);
}
else if (error.request) {
throw new MOAAPIError('Network error: No response received from MOA API');
}
else {
throw new MOAAPIError(`Request error: ${error.message}`);
}
});
}
async storeMemory(request) {
try {
const response = await this.client.post('/api/v1/memory/store', request);
return response.data;
}
catch (error) {
throw this.handleError(error, 'Failed to store memory');
}
}
async processMemory(memoryId) {
try {
const response = await this.client.post(`/api/v1/memory/process/${memoryId}`);
return response.data;
}
catch (error) {
throw this.handleError(error, `Failed to process memory ${memoryId}`);
}
}
async listMemories(params) {
try {
const response = await this.client.get('/api/v1/memory/list', { params });
return response.data;
}
catch (error) {
throw this.handleError(error, 'Failed to list memories');
}
}
async getMemoryStats() {
try {
const response = await this.client.get('/api/v1/memory/stats/overview');
return response.data;
}
catch (error) {
throw this.handleError(error, 'Failed to get memory statistics');
}
}
async searchMemories(params) {
try {
const response = await this.client.get('/api/v1/memory/search', { params });
return response.data;
}
catch (error) {
throw this.handleError(error, 'Failed to search memories');
}
}
async getMemoryTags() {
try {
const response = await this.client.get('/api/v1/memory/tags');
return response.data;
}
catch (error) {
throw this.handleError(error, 'Failed to get memory tags');
}
}
async getRecentMemories(limit) {
try {
const response = await this.client.get('/api/v1/memory/recent', { params: { limit } });
return response.data;
}
catch (error) {
throw this.handleError(error, 'Failed to get recent memories');
}
}
async bulkDeleteMemories(memoryIds) {
try {
const response = await this.client.post('/api/v1/memory/bulk-delete', memoryIds);
return response.data;
}
catch (error) {
throw this.handleError(error, 'Failed to bulk delete memories');
}
}
async exportMemories(params) {
try {
const response = await this.client.post('/api/v1/memory/export', {}, { params });
return response.data;
}
catch (error) {
throw this.handleError(error, 'Failed to export memories');
}
}
async getMemory(memoryId) {
try {
const response = await this.client.get(`/api/v1/memory/${memoryId}`);
return response.data;
}
catch (error) {
throw this.handleError(error, `Failed to get memory ${memoryId}`);
}
}
async updateMemory(memoryId, request) {
try {
const response = await this.client.put(`/api/v1/memory/${memoryId}`, request);
return response.data;
}
catch (error) {
throw this.handleError(error, `Failed to update memory ${memoryId}`);
}
}
async deleteMemory(memoryId) {
try {
const response = await this.client.delete(`/api/v1/memory/${memoryId}`);
return response.data;
}
catch (error) {
throw this.handleError(error, `Failed to delete memory ${memoryId}`);
}
}
async getMemoryProcessingStatus(memoryId) {
try {
const response = await this.client.get(`/api/v1/memory/${memoryId}/status`);
return response.data;
}
catch (error) {
throw this.handleError(error, `Failed to get processing status for memory ${memoryId}`);
}
}
async reprocessMemory(memoryId) {
try {
const response = await this.client.post(`/api/v1/memory/${memoryId}/reprocess`);
return response.data;
}
catch (error) {
throw this.handleError(error, `Failed to reprocess memory ${memoryId}`);
}
}
async queryMemories(request) {
try {
const response = await this.client.post('/api/v1/memory/query', request);
return response.data;
}
catch (error) {
throw this.handleError(error, 'Failed to query memories');
}
}
async batchQueryMemories(request) {
try {
const response = await this.client.post('/api/v1/memory/batch-query', request);
return response.data;
}
catch (error) {
throw this.handleError(error, 'Failed to batch query memories');
}
}
async getQuerySuggestions(params) {
try {
const response = await this.client.get('/api/v1/memory/suggestions', { params });
return response.data;
}
catch (error) {
throw this.handleError(error, 'Failed to get query suggestions');
}
}
async getSuggestionsAnalytics() {
try {
const response = await this.client.get('/api/v1/memory/suggestions/analytics');
return response.data;
}
catch (error) {
throw this.handleError(error, 'Failed to get suggestions analytics');
}
}
async checkMemoryHealth() {
try {
const response = await this.client.get('/api/v1/memory/health');
return response.data;
}
catch (error) {
throw this.handleError(error, 'Failed to check memory health');
}
}
async testConnection() {
try {
await this.client.get('/health');
return true;
}
catch (error) {
return false;
}
}
handleError(error, defaultMessage) {
if (error instanceof MOAAPIError) {
throw error;
}
if (error.request && !error.response) {
throw new MOAAPIError('Network error: No response received from MOA API', undefined, error);
}
if (error.response) {
const status = error.response.status;
const data = error.response.data;
throw new MOAAPIError(data?.detail || data?.message || `HTTP ${status} error`, status, error);
}
throw new MOAAPIError(defaultMessage, undefined, error);
}
}