/**
* Tool: create_company
* Create a new company in 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';
interface AttioCompanyCreateResponse {
data: {
id: {
workspace_id: string;
object_id: string;
record_id: string;
};
created_at: string;
web_url: string;
values: {
name?: Array<{ value: string }>;
domains?: Array<{ domain: string; root_domain: string }>;
description?: Array<{ value: string }>;
linkedin?: Array<{ value: string }>;
[key: string]: unknown;
};
};
}
/**
* Tool definition for MCP
*/
export const createCompanyTool: Tool = {
name: 'create_company',
description:
'Create a new company in Attio CRM. Only the company name is required. Optionally provide domains, description, and LinkedIn URL. Returns the created company record including the new record_id.',
inputSchema: {
type: 'object',
properties: {
name: {
type: 'string',
description: 'Company name (required)',
},
domains: {
type: 'array',
items: { type: 'string' },
description:
'Company domain names (e.g., ["example.com", "example.io"])',
},
description: {
type: 'string',
description: 'Company description or notes',
},
linkedin: {
type: 'string',
description:
'LinkedIn URL (e.g., "https://linkedin.com/company/example")',
},
},
required: ['name'],
},
};
/**
* Handler function for create_company tool
*/
export async function handleCreateCompany(args: {
name: string;
domains?: string[];
description?: string;
linkedin?: string;
}): Promise<CallToolResult> {
try {
const apiKey = process.env['ATTIO_API_KEY'];
if (!apiKey) {
throw new ConfigurationError('ATTIO_API_KEY not configured');
}
const { name, domains, description, linkedin } = args;
// Validate required fields
if (!name || name.trim().length === 0) {
throw new ValidationError(
'name parameter is required and cannot be empty',
'name'
);
}
const client = createAttioClient(apiKey);
// Build request body in Attio format
const values: Record<string, unknown[]> = {
name: [{ value: name.trim() }],
};
// Add optional fields if provided
if (domains && domains.length > 0) {
values['domains'] = domains
.filter((d) => d && d.trim().length > 0)
.map((domain) => ({ domain: domain.trim() }));
}
if (description && description.trim().length > 0) {
values['description'] = [{ value: description.trim() }];
}
if (linkedin && linkedin.trim().length > 0) {
values['linkedin'] = [{ value: linkedin.trim() }];
}
// Create company via Attio API
const response = await client.post<AttioCompanyCreateResponse>(
'/objects/companies/records',
{
data: {
values,
},
}
);
const company = response.data;
// Transform to clean format
const result = {
record_id: company.id.record_id,
workspace_id: company.id.workspace_id,
object_id: company.id.object_id,
created_at: company.created_at,
web_url: company.web_url,
name: company.values.name?.[0]?.value || null,
domains:
company.values.domains?.map((d) => d.domain).filter(Boolean) || [],
description: company.values.description?.[0]?.value || null,
linkedin: company.values.linkedin?.[0]?.value || null,
};
return createSuccessResponse(result);
} catch (error) {
return handleToolError(error, 'create_company');
}
}