import { z } from 'zod';
import * as kvStore from '../core/kv';
import { createErrorResponse, ErrorCodes } from '../utils/errors';
/**
* Key-value store tools for user notes
*/
export const KVGetInputSchema = z.object({
key: z.string(),
});
export async function kvGetTool(input: z.infer<typeof KVGetInputSchema>) {
try {
const value = await kvStore.kvGet(input.key);
return {
content: [
{
type: 'text',
text: JSON.stringify({
success: true,
key: input.key,
value,
found: value !== null,
}),
},
],
};
} catch (error) {
return {
content: [createErrorResponse(ErrorCodes.INTERNAL_ERROR, String(error))],
};
}
}
export const KVSetInputSchema = z.object({
key: z.string(),
value: z.any(),
});
export async function kvSetTool(input: z.infer<typeof KVSetInputSchema>) {
try {
await kvStore.kvSet(input.key, input.value);
return {
content: [
{
type: 'text',
text: JSON.stringify({
success: true,
message: `Stored: ${input.key}`,
valueType: typeof input.value,
}),
},
],
};
} catch (error) {
return {
content: [createErrorResponse(ErrorCodes.INTERNAL_ERROR, String(error))],
};
}
}
export const KVDeleteInputSchema = z.object({
key: z.string(),
});
export async function kvDeleteTool(input: z.infer<typeof KVDeleteInputSchema>) {
try {
await kvStore.kvDelete(input.key);
return {
content: [
{
type: 'text',
text: JSON.stringify({
success: true,
message: `Deleted key: ${input.key}`,
}),
},
],
};
} catch (error) {
return {
content: [createErrorResponse(ErrorCodes.INTERNAL_ERROR, String(error))],
};
}
}
export const KVListInputSchema = z.object({});
export async function kvListTool(_input: z.infer<typeof KVListInputSchema>) {
try {
const keys = await kvStore.kvList();
return {
content: [
{
type: 'text',
text: JSON.stringify({
success: true,
count: keys.length,
keys,
}),
},
],
};
} catch (error) {
return {
content: [createErrorResponse(ErrorCodes.INTERNAL_ERROR, String(error))],
};
}
}
export const KVClearInputSchema = z.object({});
export async function kvClearTool(_input: z.infer<typeof KVClearInputSchema>) {
try {
await kvStore.kvClear();
return {
content: [
{
type: 'text',
text: JSON.stringify({
success: true,
message: 'KV store cleared',
}),
},
],
};
} catch (error) {
return {
content: [createErrorResponse(ErrorCodes.INTERNAL_ERROR, String(error))],
};
}
}