Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

feat(mcp): Add optional id to upsertDataset (#13946)

Ben Bachem937e405915b7
diff --git a/web/src/__tests__/server/mcp-public-api-tools.servertest.ts b/web/src/__tests__/server/mcp-public-api-tools.servertest.ts
index 9864b23f79..9d7ca1b4b1 100644
--- a/web/src/__tests__/server/mcp-public-api-tools.servertest.ts
+++ b/web/src/__tests__/server/mcp-public-api-tools.servertest.ts
@@ -531,6 +531,43 @@ describe("MCP public API tools", () => {
expectedOutputSchema: datasetExpectedOutputSchema,
});
+ const renamedDatasetName = `mcp-dataset-renamed-${uuidv4()}`;
+ const renamedDataset = (await handleUpsertDataset(
+ {
+ id: dataset.id,
+ name: renamedDatasetName,
+ description: "Renamed MCP dataset",
+ },
+ context,
+ )) as { id: string; name: string; description: string };
+ expect(renamedDataset).toMatchObject({
+ id: dataset.id,
+ name: renamedDatasetName,
+ description: "Renamed MCP dataset",
+ });
+
+ await expect(
+ handleUpsertDataset(
+ {
+ id: "",
+ name: `mcp-dataset-empty-id-${uuidv4()}`,
+ },
+ context,
+ ),
+ ).rejects.toThrow("Validation failed");
+
+ const conflictingDatasetName = `mcp-dataset-conflict-${uuidv4()}`;
+ await handleUpsertDataset({ name: conflictingDatasetName }, context);
+ await expect(
+ handleUpsertDataset(
+ {
+ id: dataset.id,
+ name: conflictingDatasetName,
+ },
+ context,
+ ),
+ ).rejects.toThrow("Dataset name already in use");
+
const datasets = (await handleListDatasets(
{ page: 1, limit: 10 },
context,
@@ -539,7 +576,7 @@ describe("MCP public API tools", () => {
await expect(
handleGetDataset({ datasetId: dataset.id }, context),
- ).resolves.toMatchObject({ id: dataset.id, name: datasetName });
+ ).resolves.toMatchObject({ id: dataset.id, name: renamedDatasetName });
const datasetItem = (await handleUpsertDatasetItem(
{
@@ -549,7 +586,7 @@ describe("MCP public API tools", () => {
},
context,
)) as { id: string; datasetName: string };
- expect(datasetItem.datasetName).toBe(datasetName);
+ expect(datasetItem.datasetName).toBe(renamedDatasetName);
const datasetItems = (await handleListDatasetItems(
{ datasetId: dataset.id, page: 1, limit: 10 },
@@ -559,7 +596,10 @@ describe("MCP public API tools", () => {
await expect(
handleGetDatasetItem({ datasetItemId: datasetItem.id }, context),
- ).resolves.toMatchObject({ id: datasetItem.id, datasetName });
+ ).resolves.toMatchObject({
+ id: datasetItem.id,
+ datasetName: renamedDatasetName,
+ });
const runName = `mcp-run-50% accuracy %20 ${uuidv4()}`;
const runItem = (await handleCreateDatasetRunItem(
diff --git a/web/src/features/datasets/server/actions/createDataset.ts b/web/src/features/datasets/server/actions/createDataset.ts
index 5d970a93ca..c4095437d5 100644
--- a/web/src/features/datasets/server/actions/createDataset.ts
+++ b/web/src/features/datasets/server/actions/createDataset.ts
@@ -1,6 +1,7 @@
import {
DatasetNameSchema,
InvalidRequestError,
+ LangfuseConflictError,
Prisma,
} from "@langfuse/shared";
import { prisma } from "@langfuse/shared/src/db";
@@ -12,6 +13,7 @@ type DatasetJson =
| typeof Prisma.DbNull;
type UpsertDatasetInput = {
+ id?: string;
name: string;
description?: string;
metadata?: DatasetJson;
@@ -38,6 +40,10 @@ export const upsertDataset = async ({
input: UpsertDatasetInput;
projectId: string;
}) => {
+ if (input.id === "") {
+ throw new InvalidRequestError("Dataset id must not be empty");
+ }
+
const validation = DatasetNameSchema.safeParse(input.name);
if (!validation.success) {
throw new InvalidRequestError(
@@ -45,14 +51,20 @@ export const upsertDataset = async ({
);
}
- // Check if dataset exists (for UPDATE path)
const existingDataset = await prisma.dataset.findUnique({
- where: {
- projectId_name: {
- projectId,
- name: input.name,
- },
- },
+ where: input.id
+ ? {
+ id_projectId: {
+ id: input.id,
+ projectId,
+ },
+ }
+ : {
+ projectId_name: {
+ projectId,
+ name: input.name,
+ },
+ },
select: {
id: true,
inputSchema: true,
@@ -60,6 +72,24 @@ export const upsertDataset = async ({
},
});
+ if (input.id && !existingDataset) {
+ const existingDatasetWithName = await prisma.dataset.findUnique({
+ where: {
+ projectId_name: {
+ projectId,
+ name: input.name,
+ },
+ },
+ select: {
+ id: true,
+ },
+ });
+
+ if (existingDatasetWithName) {
+ throw new LangfuseConflictError("Dataset name already in use");
+ }
+ }
+
// If updating and schemas are being set, validate all existing items
if (existingDataset) {
const isSettingInputSchema = input.inputSchema !== undefined;
@@ -96,48 +126,67 @@ export const upsertDataset = async ({
}
}
- return await prisma.dataset.upsert({
- where: {
- projectId_name: {
+ const data = {
+ name: input.name,
+ description: input.description ?? undefined,
+ metadata: input.metadata ?? undefined,
+ inputSchema:
+ input.inputSchema === undefined
+ ? undefined
+ : input.inputSchema === null
+ ? Prisma.DbNull
+ : input.inputSchema,
+ expectedOutputSchema:
+ input.expectedOutputSchema === undefined
+ ? undefined
+ : input.expectedOutputSchema === null
+ ? Prisma.DbNull
+ : input.expectedOutputSchema,
+ };
+
+ try {
+ if (input.id) {
+ return await prisma.dataset.upsert({
+ where: {
+ id_projectId: {
+ id: input.id,
+ projectId,
+ },
+ },
+ create: {
+ id: input.id,
+ ...data,
+ projectId,
+ },
+ update: data,
+ });
+ }
+
+ const { name: _name, ...updateData } = data;
+
+ return await prisma.dataset.upsert({
+ where: {
+ projectId_name: {
+ projectId,
+ name: input.name,
+ },
+ },
+ create: {
+ ...data,
projectId,
- name: input.name,
},
- },
- create: {
- name: input.name,
- description: input.description ?? undefined,
- metadata: input.metadata ?? undefined,
- inputSchema:
- input.inputSchema === undefined
- ? undefined
- : input.inputSchema === null
- ? Prisma.DbNull
- : input.inputSchema,
- expectedOutputSchema:
- input.expectedOutputSchema === undefined
- ? undefined
- : input.expectedOutputSchema === null
- ? Prisma.DbNull
- : input.expectedOutputSchema,
- projectId,
- },
- update: {
- description: input.description ?? undefined,
- metadata: input.metadata ?? undefined,
- inputSchema:
- input.inputSchema === undefined
- ? undefined
- : input.inputSchema === null
- ? Prisma.DbNull
- : input.inputSchema,
- expectedOutputSchema:
- input.expectedOutputSchema === undefined
- ? undefined
- : input.expectedOutputSchema === null
- ? Prisma.DbNull
- : input.expectedOutputSchema,
- },
- });
+ update: updateData,
+ });
+ } catch (error) {
+ if (
+ error instanceof Prisma.PrismaClientKnownRequestError &&
+ error.code === "P2002"
+ ) {
+ throw new LangfuseConflictError("Dataset name already in use");
+ }
+
+ throw error;
+ }
};
export const updateDataset = async ({
diff --git a/web/src/features/datasets/server/publicDatasetService.ts b/web/src/features/datasets/server/publicDatasetService.ts
index e8e7a94c7b..6ecbbd4567 100644
--- a/web/src/features/datasets/server/publicDatasetService.ts
+++ b/web/src/features/datasets/server/publicDatasetService.ts
@@ -78,9 +78,12 @@ type GetDatasetV1Input = z.infer<typeof GetDatasetV1Query> & {
};
type CreateDatasetInput = {
- input:
+ input: (
| z.infer<typeof PostDatasetsV1Body>
- | z.infer<typeof PostDatasetsV2Body>;
+ | z.infer<typeof PostDatasetsV2Body>
+ ) & {
+ id?: string;
+ };
projectId: string;
auditScope?: DatasetAuditScope;
};
@@ -244,18 +247,26 @@ export const createDatasetForApi = async ({
}: CreateDatasetInput) => {
const existingDataset = auditScope
? await prisma.dataset.findUnique({
- where: {
- projectId_name: {
- projectId,
- name: input.name,
- },
- },
+ where: input.id
+ ? {
+ id_projectId: {
+ id: input.id,
+ projectId,
+ },
+ }
+ : {
+ projectId_name: {
+ projectId,
+ name: input.name,
+ },
+ },
})
: null;
const dataset = await upsertDataset({
input: {
name: input.name,
+ id: input.id,
description: input.description ?? undefined,
metadata: input.metadata ?? undefined,
inputSchema: input.inputSchema,
diff --git a/web/src/features/mcp/features/datasets/tools/upsertDataset.ts b/web/src/features/mcp/features/datasets/tools/upsertDataset.ts
index 3a56e1c905..c08aa8c9eb 100644
--- a/web/src/features/mcp/features/datasets/tools/upsertDataset.ts
+++ b/web/src/features/mcp/features/datasets/tools/upsertDataset.ts
@@ -8,7 +8,10 @@ import { DatasetJSONSchema } from "@langfuse/shared/src/server";
import { defineTool } from "../../../core/define-tool";
import { runMcpTool } from "../../../core/run-mcp-tool";
+const idField = z.string().min(1).optional();
+
const UpsertDatasetBaseSchema = z.object({
+ id: idField,
name: z.string(),
description: z.string().optional(),
metadata: z.any().optional(),
@@ -38,6 +41,7 @@ const DatasetJSONSchemaInput = z
const UpsertDatasetInputSchema = PostDatasetsV2Body.extend({
inputSchema: DatasetJSONSchemaInput,
expectedOutputSchema: DatasetJSONSchemaInput,
+ id: idField,
});
export const [upsertDatasetTool, handleUpsertDataset] = defineTool({