import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { z } from 'zod';
import { client } from '../client.js';
export function registerListsTools(server: McpServer): void {
server.tool(
'create_list',
'Create a new contact list that can be used to organize contacts for mailings.',
{
name: z.string().min(1).max(100).describe('List name'),
description: z.string().max(500).optional().describe('List description'),
},
async (params) => {
try {
const list = await client.post('/lists', params);
return {
content: [
{
type: 'text' as const,
text: JSON.stringify(list, null, 2),
},
],
};
} catch (error) {
return {
content: [
{
type: 'text' as const,
text: `Error: ${error instanceof Error ? error.message : 'Unknown error'}`,
},
],
isError: true,
};
}
}
);
server.tool(
'list_lists',
'Get a paginated list of all contact lists.',
{
limit: z.number().int().min(1).max(100).default(20).describe('Number of results per page'),
offset: z.number().int().min(0).default(0).describe('Pagination offset'),
search: z.string().optional().describe('Search by name'),
},
async (params) => {
try {
const result = await client.get('/lists', {
limit: params.limit,
offset: params.offset,
search: params.search,
});
return {
content: [
{
type: 'text' as const,
text: JSON.stringify(result, null, 2),
},
],
};
} catch (error) {
return {
content: [
{
type: 'text' as const,
text: `Error: ${error instanceof Error ? error.message : 'Unknown error'}`,
},
],
isError: true,
};
}
}
);
server.tool(
'get_list',
'Get detailed information about a specific contact list.',
{
listId: z.string().describe('List ID (e.g., list_abc123)'),
},
async (params) => {
try {
const list = await client.get(`/lists/${params.listId}`);
return {
content: [
{
type: 'text' as const,
text: JSON.stringify(list, null, 2),
},
],
};
} catch (error) {
return {
content: [
{
type: 'text' as const,
text: `Error: ${error instanceof Error ? error.message : 'Unknown error'}`,
},
],
isError: true,
};
}
}
);
server.tool(
'update_list',
'Update a contact list name or description.',
{
listId: z.string().describe('List ID to update'),
name: z.string().min(1).max(100).optional().describe('New list name'),
description: z.string().max(500).optional().describe('New list description'),
},
async (params) => {
try {
const { listId, ...updateData } = params;
const list = await client.patch(`/lists/${listId}`, updateData);
return {
content: [
{
type: 'text' as const,
text: JSON.stringify(list, null, 2),
},
],
};
} catch (error) {
return {
content: [
{
type: 'text' as const,
text: `Error: ${error instanceof Error ? error.message : 'Unknown error'}`,
},
],
isError: true,
};
}
}
);
server.tool(
'delete_list',
'Delete a contact list. This does not delete the contacts in the list.',
{
listId: z.string().describe('List ID to delete'),
},
async (params) => {
try {
const result = await client.delete(`/lists/${params.listId}`);
return {
content: [
{
type: 'text' as const,
text: JSON.stringify(result, null, 2),
},
],
};
} catch (error) {
return {
content: [
{
type: 'text' as const,
text: `Error: ${error instanceof Error ? error.message : 'Unknown error'}`,
},
],
isError: true,
};
}
}
);
server.tool(
'get_list_contacts',
'Get a paginated list of contacts in a specific list.',
{
listId: z.string().describe('List ID'),
limit: z.number().int().min(1).max(100).default(20).describe('Number of results per page'),
offset: z.number().int().min(0).default(0).describe('Pagination offset'),
},
async (params) => {
try {
const result = await client.get(`/lists/${params.listId}/contacts`, {
limit: params.limit,
offset: params.offset,
});
return {
content: [
{
type: 'text' as const,
text: JSON.stringify(result, null, 2),
},
],
};
} catch (error) {
return {
content: [
{
type: 'text' as const,
text: `Error: ${error instanceof Error ? error.message : 'Unknown error'}`,
},
],
isError: true,
};
}
}
);
server.tool(
'add_contacts_to_list',
'Add one or more contacts to a list (max 1000 at a time).',
{
listId: z.string().describe('List ID'),
contactIds: z.array(z.string()).min(1).max(1000).describe('Contact IDs to add'),
},
async (params) => {
try {
const { listId, contactIds } = params;
const result = await client.post(`/lists/${listId}/contacts`, {
contactIds,
});
return {
content: [
{
type: 'text' as const,
text: JSON.stringify(result, null, 2),
},
],
};
} catch (error) {
return {
content: [
{
type: 'text' as const,
text: `Error: ${error instanceof Error ? error.message : 'Unknown error'}`,
},
],
isError: true,
};
}
}
);
server.tool(
'remove_contacts_from_list',
'Remove one or more contacts from a list (max 1000 at a time).',
{
listId: z.string().describe('List ID'),
contactIds: z.array(z.string()).min(1).max(1000).describe('Contact IDs to remove'),
},
async (params) => {
try {
const { listId, contactIds } = params;
const result = await client.delete(`/lists/${listId}/contacts`, {
contactIds,
});
return {
content: [
{
type: 'text' as const,
text: JSON.stringify(result, null, 2),
},
],
};
} catch (error) {
return {
content: [
{
type: 'text' as const,
text: `Error: ${error instanceof Error ? error.message : 'Unknown error'}`,
},
],
isError: true,
};
}
}
);
}