/**
* Tool: get_note
* Get a single note by ID from Attio CRM
*/
import type { Tool, CallToolResult } from '@modelcontextprotocol/sdk/types.js';
import { createAttioClient } from '../attio-client.js';
import {
handleToolError,
createSuccessResponse,
} from '../utils/error-handler.js';
import { ConfigurationError, ValidationError } from '../utils/errors.js';
import { fetchParentRecordNames } from '../utils/fetch-parent-record-name.js';
interface AttioNoteResponse {
data: {
id: {
workspace_id: string;
note_id: string;
};
parent_object: string;
parent_record_id: string;
title: string;
content_plaintext: string;
created_by_actor: {
type: string;
id: string;
};
created_at: string;
};
}
/**
* Tool definition for MCP
*/
export const getNoteTool: Tool = {
name: 'get_note',
description:
'Get a single note by ID from Attio CRM. Returns the full note details including title, content, parent record information, and creation metadata.',
inputSchema: {
type: 'object',
properties: {
note_id: {
type: 'string',
description: 'The unique ID of the note to retrieve',
},
},
required: ['note_id'],
},
};
/**
* Handler function for get_note tool
*/
export async function handleGetNote(args: {
note_id: string;
}): Promise<CallToolResult> {
try {
const apiKey = process.env['ATTIO_API_KEY'];
if (!apiKey) {
throw new ConfigurationError('ATTIO_API_KEY not configured');
}
const { note_id } = args;
// Validate required fields
if (!note_id || note_id.trim().length === 0) {
throw new ValidationError(
'note_id parameter is required and cannot be empty',
'note_id'
);
}
const client = createAttioClient(apiKey);
// Get note via Attio API
const response = await client.get<AttioNoteResponse>(
`/notes/${note_id.trim()}`
);
const note = response.data;
// Fetch parent record name
const parentNameMap = await fetchParentRecordNames(client, [
{
parent_object: note.parent_object,
parent_record_id: note.parent_record_id,
},
]);
const nameKey = `${note.parent_object}:${note.parent_record_id}`;
// Transform to clean format
const result = {
note_id: note.id.note_id,
workspace_id: note.id.workspace_id,
parent_object: note.parent_object,
parent_record_id: note.parent_record_id,
parent_record_name: parentNameMap.get(nameKey) || null,
title: note.title,
content: note.content_plaintext,
created_by: {
type: note.created_by_actor.type,
id: note.created_by_actor.id,
},
created_at: note.created_at,
};
return createSuccessResponse(result);
} catch (error) {
return handleToolError(error, 'get_note');
}
}