Skip to content

Job application tracker and AI-powered job search assistant. Helps job seekers manage their search journey with AI resume review, job matching, task logging, and application analytics, all while keeping your data private.

feat(mcp): add upsert-by-URL routing to add_job

Khuram Niaz49273b2bbe5a
diff --git a/__tests__/mcpAddJob.spec.ts b/__tests__/mcpAddJob.spec.ts
index 6322942..0ec317d 100644
--- a/__tests__/mcpAddJob.spec.ts
+++ b/__tests__/mcpAddJob.spec.ts
@@ -1,5 +1,6 @@
import { handleAddJob } from "@/lib/mcp/tools/addJob";
import { createJobFromNames } from "@/lib/jobs/createJobFromNames";
+import { updateJobFromNames } from "@/lib/jobs/updateJobFromNames";
import { getDefaultResumeForUser } from "@/lib/jobs/getDefaultResumeForUser";
import { preprocessResume } from "@/lib/ai/tools/preprocessing";
import { checkMcpRateLimit } from "@/lib/mcp/rate-limit";
@@ -8,6 +9,10 @@ vi.mock("@/lib/jobs/createJobFromNames", () => ({
createJobFromNames: vi.fn(),
}));
+vi.mock("@/lib/jobs/updateJobFromNames", () => ({
+ updateJobFromNames: vi.fn(),
+}));
+
vi.mock("@/lib/jobs/getDefaultResumeForUser", () => ({
getDefaultResumeForUser: vi.fn(),
}));
@@ -161,3 +166,97 @@ describe("handleAddJob match gating", () => {
expect(createJobFromNames).not.toHaveBeenCalled();
});
});
+
+describe("handleAddJob upsert routing", () => {
+ const duplicateResult = {
+ created: false,
+ duplicateOf: { id: "job-9", title: "Engineer", company: "Acme" },
+ resolutions: [],
+ message: 'Duplicate detected — existing job "Engineer" at "Acme" (id: job-9).',
+ };
+
+ beforeEach(() => {
+ vi.clearAllMocks();
+ (checkMcpRateLimit as any).mockReturnValue({ allowed: true, resetIn: 0 });
+ });
+
+ it("updates the existing job instead of duplicating when upsert is true", async () => {
+ (createJobFromNames as any).mockResolvedValue(duplicateResult);
+ (updateJobFromNames as any).mockResolvedValue({
+ updated: true,
+ jobId: "job-9",
+ descriptionChanged: true,
+ descriptionCompleteness: "full",
+ resolutions: [],
+ message: "Job job-9 updated. Fields changed: description.",
+ });
+ mockUsableResume();
+
+ const result = await handleAddJob(
+ { ...baseInput, upsert: true } as any,
+ "user-1",
+ "my-token",
+ );
+ const text = result.content[0].text;
+
+ expect(updateJobFromNames).toHaveBeenCalledWith(
+ expect.objectContaining({ jobId: "job-9", jobDescription: fullDescription }),
+ "user-1",
+ );
+ expect(text).toContain("Job job-9 updated");
+ expect(text).toContain("save_match_result");
+ expect(text).not.toContain("Duplicate detected");
+ });
+
+ it("does not pass add-only fields through to the update", async () => {
+ (createJobFromNames as any).mockResolvedValue(duplicateResult);
+ (updateJobFromNames as any).mockResolvedValue({
+ updated: true,
+ jobId: "job-9",
+ descriptionChanged: false,
+ descriptionCompleteness: "full",
+ resolutions: [],
+ message: "Job job-9 updated.",
+ });
+
+ await handleAddJob(
+ { ...baseInput, upsert: true, allowDuplicate: false } as any,
+ "user-1",
+ "my-token",
+ );
+
+ const passed = (updateJobFromNames as any).mock.calls[0][0];
+ expect(passed).not.toHaveProperty("upsert");
+ expect(passed).not.toHaveProperty("allowDuplicate");
+ expect(passed).not.toHaveProperty("createdVia");
+ });
+
+ it("reports the duplicate normally when upsert is not set", async () => {
+ (createJobFromNames as any).mockResolvedValue(duplicateResult);
+
+ const result = await handleAddJob(baseInput as any, "user-1", "my-token");
+
+ expect(updateJobFromNames).not.toHaveBeenCalled();
+ expect(result.content[0].text).toContain("Duplicate detected");
+ });
+
+ it("explains when the duplicate is a web-app job that MCP cannot update", async () => {
+ (createJobFromNames as any).mockResolvedValue(duplicateResult);
+ (updateJobFromNames as any).mockResolvedValue({
+ updated: false,
+ jobId: "job-9",
+ descriptionChanged: false,
+ descriptionCompleteness: null,
+ resolutions: [],
+ message: "Job not found, not owned by this token's user, or not eligible",
+ });
+
+ const result = await handleAddJob(
+ { ...baseInput, upsert: true } as any,
+ "user-1",
+ "my-token",
+ );
+
+ expect(result.content[0].text).toContain("not eligible");
+ });
+});
diff --git a/src/lib/mcp/tools/addJob.ts b/src/lib/mcp/tools/addJob.ts
index 0cdcdf3..2f73b13 100644
--- a/src/lib/mcp/tools/addJob.ts
+++ b/src/lib/mcp/tools/addJob.ts
@@ -1,6 +1,7 @@
import { z } from "zod";
import { McpAddJobSchema } from "@/models/mcp.schema";
import { createJobFromNames } from "@/lib/jobs/createJobFromNames";
+import { updateJobFromNames } from "@/lib/jobs/updateJobFromNames";
import { checkMcpRateLimit } from "@/lib/mcp/rate-limit";
import { buildMatchOffer, composeOfferMessage } from "@/lib/mcp/tools/matchDirective";
@@ -24,9 +25,13 @@ export async function handleAddJob(
);
if (!result.created || !result.jobId) {
+ if (input.upsert && result.duplicateOf) {
+ return upsertExisting(result.duplicateOf.id, input, userId);
+ }
const text =
result.message +
- " No match offered for duplicates — call update_job to enrich the existing record, then it will be offered.";
+ " No match offered for duplicates — pass upsert: true (or call update_job)" +
+ " to enrich the existing record instead.";
return { content: [{ type: "text", text }] };
}
@@ -45,3 +50,29 @@ export async function handleAddJob(
};
}
}
+
+// Routes a detected duplicate into the patch path. Add-only flags are
+// dropped: upsert/allowDuplicate mean nothing to an update, and createdVia
+// must not be rewritten to whichever token happened to re-submit the job.
+async function upsertExisting(
+ jobId: string,
+ input: z.infer<typeof McpAddJobSchema>,
+ userId: string,
+): Promise<{ content: Array<{ type: "text"; text: string }> }> {
+ const { upsert, allowDuplicate, ...fields } = input;
+ const result = await updateJobFromNames({ jobId, ...fields }, userId);
+
+ if (!result.updated || !result.descriptionChanged) {
+ return { content: [{ type: "text", text: result.message }] };
+ }
+
+ const offer = await buildMatchOffer(
+ result.jobId,
+ userId,
+ result.descriptionCompleteness ?? "title-only",
+ "update",
+ );
+ return {
+ content: [{ type: "text", text: composeOfferMessage(result.message, offer) }],
+ };
+}
diff --git a/src/models/mcp.schema.ts b/src/models/mcp.schema.ts
index ddd474d..7b0f551 100644
--- a/src/models/mcp.schema.ts
+++ b/src/models/mcp.schema.ts
@@ -33,7 +33,23 @@ export const McpAddJobInputShape = {
jobUrl: z.string().url().optional().describe("Direct URL to the job posting"),
salaryRange: z.string().optional().describe("Salary range as a free-form string, e.g. '$120k–$150k' or '100,000 CAD'"),
tags: z.array(z.string()).optional().describe("Skills required for the job (max 10 applied, extras are dropped). Tags are created if they don't exist. e.g. ['React', 'TypeScript', 'Node.js']"),
- allowDuplicate: z.boolean().optional(),
+ allowDuplicate: z
+ .boolean()
+ .optional()
+ .describe(
+ "Force-create even if a matching job already exists. Prefer upsert:true " +
+ "for re-runs of the same search; use this only for a genuinely " +
+ "different posting.",
+ ),
+ upsert: z
+ .boolean()
+ .optional()
+ .describe(
+ "If a job with this URL (or same company+title within the dedupe " +
+ "window) already exists, update it with the fields supplied here " +
+ "instead of reporting a duplicate. Recommended for scheduled searches " +
+ "that resurface the same posting daily.",
+ ),
};
// Full schema with transforms for parsing raw MCP input in the handler