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): gate match directive on description completeness

Khuram Niazfc67810c338e
diff --git a/__tests__/mcpAddJob.spec.ts b/__tests__/mcpAddJob.spec.ts
index 2cbdd4f..6322942 100644
--- a/__tests__/mcpAddJob.spec.ts
+++ b/__tests__/mcpAddJob.spec.ts
@@ -20,136 +20,144 @@ vi.mock("@/lib/mcp/rate-limit", () => ({
checkMcpRateLimit: vi.fn(() => ({ allowed: true, resetIn: 0 })),
}));
-const longDescription = "a".repeat(250);
-const shortDescription = "Short JD.";
+const words = (n: number) => Array.from({ length: n }, () => "word").join(" ");
+const fullDescription = words(200);
+const partialDescription = words(60);
+const stubDescription = "Frontend Developer, $120k, Remote.";
const baseInput = {
company: "Acme",
jobTitle: "Engineer",
- jobDescription: longDescription,
+ jobDescription: fullDescription,
};
+function mockCreated(completeness: string) {
+ (createJobFromNames as any).mockResolvedValue({
+ created: true,
+ jobId: "job-1",
+ resolutions: [],
+ descriptionCompleteness: completeness,
+ message: "Created Acme; Created Engineer. Job created (id: job-1).",
+ });
+}
+
+function mockUsableResume() {
+ (getDefaultResumeForUser as any).mockResolvedValue({
+ id: "resume-1",
+ title: "My Resume",
+ });
+ (preprocessResume as any).mockResolvedValue({
+ success: true,
+ data: { normalizedText: "NORMALIZED RESUME TEXT", metadata: {}, isValid: true },
+ });
+}
+
describe("handleAddJob match gating", () => {
beforeEach(() => {
vi.clearAllMocks();
+ (checkMcpRateLimit as any).mockReturnValue({ allowed: true, resetIn: 0 });
});
- it("appends resume text + save_match_result directive when created + substantive + default resume usable", async () => {
- (createJobFromNames as any).mockResolvedValue({
- created: true,
- jobId: "job-1",
- resolutions: [],
- message: "Created Acme; Created Engineer. Job created (id: job-1).",
- });
- (getDefaultResumeForUser as any).mockResolvedValue({ id: "resume-1", title: "My Resume" });
- (preprocessResume as any).mockResolvedValue({
- success: true,
- data: { normalizedText: "NORMALIZED RESUME TEXT", metadata: {}, isValid: true },
- });
+ it("issues the full directive for a full description with a usable resume", async () => {
+ mockCreated("full");
+ mockUsableResume();
const result = await handleAddJob(baseInput as any, "user-1", "my-token");
const text = result.content[0].text;
expect(text).toContain("Job created (id: job-1)");
expect(text).toContain("NORMALIZED RESUME TEXT");
- expect(text).toContain("job-1");
expect(text).toContain("save_match_result");
- expect(text).toContain("SCORES: match=<0-100> recommendation=<strong|good|partial|weak>");
- // Directive requests the four structured sections (richer match body).
+ expect(text).toContain(
+ "SCORES: match=<0-100> recommendation=<strong|good|partial|weak>",
+ );
expect(text).toContain("## Overall Fit");
expect(text).toContain("## Key Strengths");
expect(text).toContain("## Gaps / Risks");
expect(text).toContain("## Recommendation");
- // Directive threads the resumeId back for save_match_result provenance.
expect(text).toContain('"resumeId": "resume-1"');
+ expect(text).not.toContain("PARTIAL DESCRIPTION WARNING");
});
- it("adds a short-description note and no directive when description is under the threshold", async () => {
- (createJobFromNames as any).mockResolvedValue({
- created: true,
- jobId: "job-1",
- resolutions: [],
- message: "Created Acme; Created Engineer. Job created (id: job-1).",
- });
+ it("issues a directive WITH a provisional warning for a partial description", async () => {
+ mockCreated("partial");
+ mockUsableResume();
+
+ const result = await handleAddJob(
+ { ...baseInput, jobDescription: partialDescription } as any,
+ "user-1",
+ "my-token",
+ );
+ const text = result.content[0].text;
+
+ expect(text).toContain("save_match_result");
+ expect(text).toContain("PARTIAL DESCRIPTION WARNING");
+ expect(text).toContain("update_job");
+ });
+
+ it("refuses to offer a match for a title-only description", async () => {
+ mockCreated("title-only");
const result = await handleAddJob(
- { ...baseInput, jobDescription: shortDescription } as any,
- "user-2",
+ { ...baseInput, jobDescription: stubDescription } as any,
+ "user-1",
"my-token",
);
const text = result.content[0].text;
- expect(text).toContain("Description too short to match");
+ expect(text).toContain("Job created (id: job-1)");
+ expect(text).toContain("too thin to score");
+ expect(text).toContain("update_job");
expect(text).not.toContain("save_match_result");
expect(getDefaultResumeForUser).not.toHaveBeenCalled();
});
- it("adds an actionable note when no default resume is set", async () => {
- (createJobFromNames as any).mockResolvedValue({
- created: true,
- jobId: "job-1",
- resolutions: [],
- message: "Created Acme; Created Engineer. Job created (id: job-1).",
- });
+ it("notes a missing default resume and still reports the job as created", async () => {
+ mockCreated("full");
(getDefaultResumeForUser as any).mockResolvedValue(null);
- const result = await handleAddJob(baseInput as any, "user-3", "my-token");
+ const result = await handleAddJob(baseInput as any, "user-1", "my-token");
const text = result.content[0].text;
+ expect(text).toContain("Job created (id: job-1)");
expect(text).toContain("No default resume set");
expect(text).not.toContain("save_match_result");
- expect(preprocessResume).not.toHaveBeenCalled();
});
- it("adds a distinct note when the default resume exists but fails preprocessing", async () => {
- (createJobFromNames as any).mockResolvedValue({
- created: true,
- jobId: "job-1",
- resolutions: [],
- message: "Created Acme; Created Engineer. Job created (id: job-1).",
- });
- (getDefaultResumeForUser as any).mockResolvedValue({ id: "resume-1", title: "My Resume" });
- (preprocessResume as any).mockResolvedValue({
- success: false,
- error: { code: "TOO_SHORT", message: "Resume is too short" },
- });
+ it("notes an unusable default resume", async () => {
+ mockCreated("full");
+ (getDefaultResumeForUser as any).mockResolvedValue({ id: "resume-1" });
+ (preprocessResume as any).mockResolvedValue({ success: false });
- const result = await handleAddJob(baseInput as any, "user-4", "my-token");
+ const result = await handleAddJob(baseInput as any, "user-1", "my-token");
const text = result.content[0].text;
- expect(text).toContain("Default resume couldn't be used for matching");
- expect(text).not.toContain("No default resume set");
+ expect(text).toContain("couldn't be used for matching");
expect(text).not.toContain("save_match_result");
});
- it("does not offer a match on a duplicate, and explains why", async () => {
+ it("returns the duplicate message with no match offer", async () => {
(createJobFromNames as any).mockResolvedValue({
created: false,
- duplicateOf: { id: "job-existing", title: "Engineer", company: "Acme" },
+ duplicateOf: { id: "job-9", title: "Engineer", company: "Acme" },
resolutions: [],
- message: 'Duplicate detected — existing job "Engineer" at "Acme" (id: job-existing).',
+ message: 'Duplicate detected — existing job "Engineer" at "Acme" (id: job-9).',
});
- const result = await handleAddJob(baseInput as any, "user-5", "my-token");
+ const result = await handleAddJob(baseInput as any, "user-1", "my-token");
const text = result.content[0].text;
expect(text).toContain("Duplicate detected");
- expect(text).toContain("No match offered for duplicates");
+ expect(text).not.toContain("save_match_result");
expect(getDefaultResumeForUser).not.toHaveBeenCalled();
- expect(preprocessResume).not.toHaveBeenCalled();
});
- it("returns a rate-limit message and never creates a job when the limit is exceeded", async () => {
- (checkMcpRateLimit as any).mockReturnValueOnce({
- allowed: false,
- resetIn: 60000,
- });
+ it("short-circuits when rate limited", async () => {
+ (checkMcpRateLimit as any).mockReturnValue({ allowed: false, resetIn: 5000 });
- const result = await handleAddJob(baseInput as any, "user-6", "my-token");
- const text = result.content[0].text;
+ const result = await handleAddJob(baseInput as any, "user-1", "my-token");
- expect(text).toContain("Rate limit exceeded");
- expect(text).toContain("60s");
+ expect(result.content[0].text).toContain("Rate limit exceeded");
expect(createJobFromNames).not.toHaveBeenCalled();
});
});
diff --git a/src/lib/mcp/tools/addJob.ts b/src/lib/mcp/tools/addJob.ts
index 3c072ee..0cdcdf3 100644
--- a/src/lib/mcp/tools/addJob.ts
+++ b/src/lib/mcp/tools/addJob.ts
@@ -1,42 +1,8 @@
import { z } from "zod";
-import { APP_CONSTANTS } from "@/lib/constants";
import { McpAddJobSchema } from "@/models/mcp.schema";
import { createJobFromNames } from "@/lib/jobs/createJobFromNames";
import { checkMcpRateLimit } from "@/lib/mcp/rate-limit";
-import { getDefaultResumeForUser } from "@/lib/jobs/getDefaultResumeForUser";
-import { preprocessResume } from "@/lib/ai/tools/preprocessing";
-
-function buildMatchDirective(
- jobId: string,
- resumeId: string,
- normalizedResumeText: string,
-): string {
- return (
- `You just added a job to JobSync (job id: ${jobId}). Produce a job-fit ` +
- `match of this job against the user's default resume, shown below.\n\n` +
- `DEFAULT RESUME (normalized):\n${normalizedResumeText}\n\n` +
- `INSTRUCTIONS:\n` +
- `- Analyze fit between the resume above and the job description you just\n` +
- ` submitted in this same add_job call.\n` +
- `- Use ONLY the resume and that job description. Do not invent employers,\n` +
- ` skills, titles, dates, or credentials that are not present in them.\n` +
- `- Score honestly. If the resume lacks the information needed to judge fit,\n` +
- ` still emit a SCORES line with a low score and a "weak" recommendation and\n` +
- ` say what is missing — do NOT fabricate a match.\n` +
- `- Produce, in this exact order:\n` +
- ` 1. A single first line, exactly:\n` +
- ` SCORES: match=<0-100> recommendation=<strong|good|partial|weak>\n` +
- ` 2. A markdown body with these four sections, each at least a few\n` +
- ` sentences of specific, evidence-based analysis (cite concrete\n` +
- ` skills, titles, and years from the resume and job description):\n` +
- ` ## Overall Fit\n` +
- ` ## Key Strengths\n` +
- ` ## Gaps / Risks\n` +
- ` ## Recommendation\n` +
- `- Then call save_match_result with:\n` +
- ` { "jobId": "${jobId}", "resumeId": "${resumeId}", "matchText": "<the full SCORES line + markdown body>" }`
- );
-}
+import { buildMatchOffer, composeOfferMessage } from "@/lib/mcp/tools/matchDirective";
export async function handleAddJob(
input: z.infer<typeof McpAddJobSchema>,
@@ -60,39 +26,19 @@ export async function handleAddJob(
if (!result.created || !result.jobId) {
const text =
result.message +
- " No match offered for duplicates — you can match it in the web app.";
- return { content: [{ type: "text", text }] };
- }
-
- if (input.jobDescription.length < APP_CONSTANTS.MCP_MATCH_MIN_DESCRIPTION_LENGTH) {
- const text =
- result.message +
- " Description too short to match (under 200 characters) — the job was still added.";
- return { content: [{ type: "text", text }] };
- }
-
- const resume = await getDefaultResumeForUser(userId);
- if (!resume) {
- const text =
- result.message +
- " No default resume set — set one in Profile → Resumes to enable automatic matching.";
- return { content: [{ type: "text", text }] };
- }
-
- const pre = await preprocessResume(resume);
- if (!pre.success) {
- const text =
- result.message +
- " Default resume couldn't be used for matching (it may be too short or missing content) — check it in Profile → Resumes.";
+ " No match offered for duplicates — call update_job to enrich the existing record, then it will be offered.";
return { content: [{ type: "text", text }] };
}
- const directive = buildMatchDirective(
+ const offer = await buildMatchOffer(
result.jobId,
- resume.id!,
- pre.data.normalizedText,
+ userId,
+ result.descriptionCompleteness ?? "title-only",
+ "add",
);
- return { content: [{ type: "text", text: `${result.message}\n\n${directive}` }] };
+ return {
+ content: [{ type: "text", text: composeOfferMessage(result.message, offer) }],
+ };
} catch (err: any) {
return {
content: [{ type: "text", text: `Error: ${err?.message ?? "Unknown error"}` }],
diff --git a/src/lib/mcp/tools/matchDirective.ts b/src/lib/mcp/tools/matchDirective.ts
new file mode 100644
index 0000000..a800e89
--- /dev/null
+++ b/src/lib/mcp/tools/matchDirective.ts
@@ -0,0 +1,110 @@
+import { APP_CONSTANTS } from "@/lib/constants";
+import { getDefaultResumeForUser } from "@/lib/jobs/getDefaultResumeForUser";
+import { preprocessResume } from "@/lib/ai/tools/preprocessing";
+import type { DescriptionCompleteness } from "@/models/job.model";
+
+export function buildMatchDirective(
+ jobId: string,
+ resumeId: string,
+ normalizedResumeText: string,
+ completeness: DescriptionCompleteness,
+ context: "add" | "update",
+): string {
+ const source =
+ context === "add"
+ ? "the job description you just submitted in this same add_job call"
+ : "the job description now stored on this job (as just updated)";
+
+ const warning =
+ completeness === "partial"
+ ? `\n\nPARTIAL DESCRIPTION WARNING: this posting is under ` +
+ `${APP_CONSTANTS.DESCRIPTION_FULL_MIN_WORDS} words, so any score you ` +
+ `produce is provisional and will be labelled as such in JobSync. Weight ` +
+ `your confidence accordingly, say plainly in the body what the ` +
+ `description does not tell you, and if you can fetch the full posting, ` +
+ `call update_job with it first and score the enriched version instead.`
+ : "";
+
+ return (
+ `Produce a job-fit match of JobSync job id ${jobId} against the user's ` +
+ `default resume, shown below.${warning}\n\n` +
+ `DEFAULT RESUME (normalized):\n${normalizedResumeText}\n\n` +
+ `INSTRUCTIONS:\n` +
+ `- Analyze fit between the resume above and ${source}.\n` +
+ `- Use ONLY the resume and that job description. Do not invent employers,\n` +
+ ` skills, titles, dates, or credentials that are not present in them.\n` +
+ `- Score honestly. If the resume lacks the information needed to judge fit,\n` +
+ ` still emit a SCORES line with a low score and a "weak" recommendation and\n` +
+ ` say what is missing — do NOT fabricate a match.\n` +
+ `- Produce, in this exact order:\n` +
+ ` 1. A single first line, exactly:\n` +
+ ` SCORES: match=<0-100> recommendation=<strong|good|partial|weak>\n` +
+ ` 2. A markdown body with these four sections, each at least a few\n` +
+ ` sentences of specific, evidence-based analysis (cite concrete\n` +
+ ` skills, titles, and years from the resume and job description):\n` +
+ ` ## Overall Fit\n` +
+ ` ## Key Strengths\n` +
+ ` ## Gaps / Risks\n` +
+ ` ## Recommendation\n` +
+ `- Then call save_match_result with:\n` +
+ ` { "jobId": "${jobId}", "resumeId": "${resumeId}", "matchText": "<the full SCORES line + markdown body>" }`
+ );
+}
+
+// Decides whether a match is offered at all, and returns either the
+// directive or the one-line note explaining why not. Callers append a
+// "directive" with a blank line and a "note" with a single space.
+export async function buildMatchOffer(
+ jobId: string,
+ userId: string,
+ completeness: DescriptionCompleteness,
+ context: "add" | "update",
+): Promise<{ kind: "directive" | "note"; text: string }> {
+ if (completeness === "title-only") {
+ return {
+ kind: "note",
+ text:
+ `The description is too thin to score (under ` +
+ `${APP_CONSTANTS.DESCRIPTION_PARTIAL_MIN_WORDS} words) — the job was ` +
+ `saved, but no fit analysis was requested. Fetch the full posting and ` +
+ `call update_job with jobId "${jobId}" to get one.`,
+ };
+ }
+
+ const resume = await getDefaultResumeForUser(userId);
+ if (!resume) {
+ return {
+ kind: "note",
+ text: "No default resume set — set one in Profile → Resumes to enable automatic matching.",
+ };
+ }
+
+ const pre = await preprocessResume(resume);
+ if (!pre.success) {
+ return {
+ kind: "note",
+ text: "Default resume couldn't be used for matching (it may be too short or missing content) — check it in Profile → Resumes.",
+ };
+ }
+
+ return {
+ kind: "directive",
+ text: buildMatchDirective(
+ jobId,
+ resume.id!,
+ pre.data.normalizedText,
+ completeness,
+ context,
+ ),
+ };
+}
+
+// Shared by add_job and update_job so both compose the offer identically.
+export function composeOfferMessage(
+ message: string,
+ offer: { kind: "directive" | "note"; text: string },
+): string {
+ return offer.kind === "directive"
+ ? `${message}\n\n${offer.text}`
+ : `${message} ${offer.text}`;
+}