Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ GOOGLE_DRIVE_CLIENT_SECRET=
GOOGLE_DRIVE_REDIRECT_URI=http://localhost:8080/api/v1/google-drive/oauth/callback
GOOGLE_DRIVE_OAUTH_COMPLETE_URL=http://localhost:5173/settings/google-drive/callback

# Integrations (TinyFish Fetch + Gmail drafts). Generate with: openssl rand -base64 32
APP_INTEGRATIONS_ENCRYPTION_KEY=
# Gmail reuses the Google Drive OAuth client unless GMAIL_CLIENT_ID/GMAIL_CLIENT_SECRET are set.
GMAIL_REDIRECT_URI=http://localhost:8080/api/v1/integrations/gmail/oauth/callback
GMAIL_OAUTH_COMPLETE_URL=http://localhost:5173/account

# Ask ApplyWell: choose none (disabled), google-genai (Gemini), or openai (OpenRouter).
APP_ASSISTANT_ENABLED=false
# This is the Spring AI ChatModel provider ID: openai uses OpenRouter's compatible API.
Expand Down
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,15 @@ Response:
}
```

## TinyFish Fetch and Gmail drafts

Users can read public URLs (for example a LinkedIn job post) through their own TinyFish Fetch key
and create Gmail drafts with the generated resume attached. Both go through Applywell's own MCP
server (`Fetch-Url`, `List-Email-Accounts`, `Create-Email-Draft`) and REST API under
`/api/v1/integrations/**`. Drafts are never sent automatically, and creating one never marks an
application as applied. For setup (`APP_INTEGRATIONS_ENCRYPTION_KEY`, Gmail API and OAuth
consent requirements) and tool contracts, see [docs/integrations.md](docs/integrations.md).

## MCP Server (Model Context Protocol)

The backend exposes all core domain services as MCP tools via a **Streamable HTTP** endpoint at `POST /mcp/messages`. Any MCP-compatible AI client (Claude Desktop, Claude.ai, `mcp-cli`, etc.) can call these tools on behalf of an authenticated user.
Expand Down
155 changes: 155 additions & 0 deletions docs/integrations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
# Integrations: TinyFish Fetch and Gmail drafts

Issue #113. Applywell's own MCP server can read a job post from a URL and create a Gmail draft
with the generated resume attached, so AI clients no longer need the TinyFish or Draft Email Hub
plugins. The AI client still writes the resume content and the email body; Applywell only
fetches pages, stores and renders documents, and creates drafts.

Each integration is optional per user and works on its own. Nothing here sends email or changes
an application's status.

## Typical flow

1. The AI client calls `Fetch-Url` with the shared URL and gets the page as Markdown.
2. The AI client reads the job post and runs the usual application and resume tools
(`Create-Application`, `Generate-Application-Resume`, and so on).
3. The AI client calls `List-Email-Accounts`, then `Create-Email-Draft` with the recipient,
subject, HTML body, account or profile, and the `applicationId` whose PDF should be attached.
4. The user reviews the draft in Gmail and sends it.

## Server configuration

| Variable | Purpose |
| --- | --- |
| `APP_INTEGRATIONS_ENCRYPTION_KEY` | **Required.** Base64 of 32 random bytes (`openssl rand -base64 32`). AES-256-GCM key for user API keys and OAuth tokens at rest. If it is missing, users cannot save integration secrets. If it is rotated, stored secrets become unreadable: users must re-enter their TinyFish key and reconnect Gmail. |
| `TINYFETCH_BASE_URL` | Default `https://api.fetch.tinyfish.ai`. |
| `TINYFETCH_MAX_PER_MINUTE_LIMIT` / `TINYFETCH_MAX_DAILY_LIMIT` | Ceilings that users cannot exceed. Defaults 150/min and 1000/day (TinyFish free tier as of 2026-11-23). Lower them if your account has a smaller quota. |
| `TINYFETCH_READ_TIMEOUT`, `TINYFETCH_MAX_ATTEMPTS`, `TINYFETCH_RETRY_BASE_DELAY`, `TINYFETCH_MAX_RETRY_DELAY` | Timeout and bounded retry with exponential backoff on 429/5xx. `Retry-After` is honoured up to the max delay. |
| `TINYFETCH_CACHE_TTL`, `TINYFETCH_MAX_CONTENT_CHARS` | Per-user in-memory cache lifetime (default 15m) and the content truncation limit. |
| `GMAIL_CLIENT_ID` / `GMAIL_CLIENT_SECRET` | Default to the Google Drive OAuth client. |
| `GMAIL_REDIRECT_URI` | Default `${APP_API_BASE_URL}/api/v1/integrations/gmail/oauth/callback`. It must be registered on the OAuth client. |
| `GMAIL_OAUTH_COMPLETE_URL` | The frontend page the callback redirects to, e.g. `https://applywell.hugojava.dev/account`. The redirect adds `integration=gmail&status=success|error&message=...`. |
| `GMAIL_MAX_ACCOUNTS_PER_USER`, `GMAIL_MAX_ATTACHMENT_BYTES`, `GMAIL_DRAFT_PENDING_TIMEOUT` | Limits. |

### Google Cloud / OAuth requirements

- Enable the **Gmail API** in the same Google Cloud project as the Drive OAuth client, or create
a dedicated client and set `GMAIL_CLIENT_ID`/`GMAIL_CLIENT_SECRET`.
- Add the redirect URI above to the client's *Authorized redirect URIs*.
- The only scope requested is `https://www.googleapis.com/auth/gmail.compose`. It is the
narrowest scope that allows `users.drafts.create`, and it also covers `users.getProfile`, which
is used to identify the mailbox. Google classifies `gmail.compose` as a **restricted** scope:
- While the consent screen is in *Testing*, only listed test users can connect, and refresh
tokens expire after 7 days.
- Public availability requires OAuth app verification, which includes a security assessment for
restricted scopes. See Google's
[restricted scope verification](https://developers.google.com/identity/protocols/oauth2/production-readiness/restricted-scope-verification)
and [Gmail API scopes](https://developers.google.com/workspace/gmail/api/auth/scopes) pages.
- The flow uses `access_type=offline`, `prompt=consent` and PKCE (S256), plus a single-use
`state` that expires after 10 minutes. Signing in to Applywell with Google is **not** treated
as Gmail access: each mailbox goes through its own consent.
- Disconnecting an account calls Google's revoke endpoint (best effort) and deletes the stored
tokens. A refresh that fails with `invalid_grant` flags the account as needing reconnection.

## REST API

All endpoints require authentication. Endpoints marked *session only* reject OAuth tokens issued
to MCP/GPT clients, so an AI client can use the integrations but cannot replace credentials or
connect and disconnect mailboxes.

### TinyFish Fetch: `/api/v1/integrations/tinyfetch`

| Method | Path | Notes |
| --- | --- | --- |
| GET | `/` | Settings. The key is never returned, only a hint such as `••••1234`. Includes `usedToday`. |
| PUT | `/` | *Session only.* `{ apiKey?, enabled?, perMinuteLimit?, dailyLimit? }`. `apiKey` is required on first setup. |
| DELETE | `/` | *Session only.* Removes the settings and the stored key. |
| POST | `/test` | Fetches `TINYFETCH_TEST_URL` once (counts toward quota) and records the result. |
| POST | `/fetch` | `{ url, bypassCache? }` → same payload as the `Fetch-Url` tool. |

### Gmail: `/api/v1/integrations/gmail`

| Method | Path | Notes |
| --- | --- | --- |
| GET | `/` | `{ serverConfigured, maxAccounts, scopes, accounts[] }` |
| POST | `/oauth/start` | *Session only.* `{ profileName? }` → `{ authorizationUrl }`. Reconnecting an already connected mailbox updates it in place. |
| GET | `/oauth/callback` | Public; called by Google. |
| PATCH | `/accounts/{id}` | *Session only.* `{ profileName }` |
| POST | `/accounts/{id}/test` | Calls `users.getProfile` with the stored grant. |
| DELETE | `/accounts/{id}` | *Session only.* Revokes the grant and deletes the account. |
| POST | `/drafts` | Same contract as `Create-Email-Draft`. Returns 201 for a new draft, 200 for a deduplicated one. |

## MCP tools

### `Fetch-Url`

Input: `url` (required), `bypassCache` (optional).

Output:

```json
{
"status": "OK | PARTIAL | BLOCKED | FAILED",
"url": "https://...", "finalUrl": "https://...", "title": "...",
"markdown": "...", "metadata": { "...": "..." },
"contentLength": 1234, "truncated": false, "cached": false,
"manualInputRequired": false, "message": null,
"contentNotice": "Untrusted third-party page content...",
"fetchedAt": "2026-11-24T10:00:00"
}
```

- `PARTIAL` means fewer than 200 characters came back, usually a login wall. `BLOCKED` means
TinyFish reported bot or login blocking. `FAILED` covers timeouts and upstream errors. In all
three cases `manualInputRequired=true`, and the client should ask the user to paste the job
description.
- Errors: not configured or disabled (400), invalid or private URL (400, SSRF guard: only http(s)
on default ports, no credentials in the URL, every resolved address must be public), API key
rejected (400), user or provider quota reached (429 with `Retry-After` and codes
`TINYFETCH_MINUTE_LIMIT`, `TINYFETCH_DAILY_LIMIT`, `TINYFETCH_PROVIDER_RATE_LIMIT`,
`TINYFETCH_PROVIDER_QUOTA`).
- Concurrent identical requests from the same user share one provider call. The cache is
per user. Cache hits do not count toward quota.
- Page content is data. Nothing in Applywell acts on it, and the tool description tells the AI
client not to either.

### `List-Email-Accounts`

Returns `[{ id, email, profileName, connectedAt, lastTestedAt, reauthorizationRequired }]`.

### `Create-Email-Draft`

Input: `to[]`, `subject`, `htmlBody` (required); `accountId` or `profileName` (required when
more than one account is connected); `applicationId`, `attachResume` (default true when
`applicationId` is set), `cc[]`, `bcc[]`, `idempotencyKey` (all optional).

Output: `{ status: CREATED | ALREADY_CREATED, draftId, messageId, accountId, accountEmail,
profileName, applicationId, attachmentFileName, idempotencyKey, deduplicated, gmailDraftsUrl, message }`.

- The resume PDF is exported internally from the application's generated Google Doc through the
user's Drive connection. No public URL is used, and application ownership is checked.
- The message is `multipart/mixed`: an HTML part with a plain-text alternative, plus the PDF.
Header values containing CR/LF are rejected.
- **Idempotency.** Without `idempotencyKey`, the key is a SHA-256 of the normalized request.
A ledger row is committed before Gmail is called:
- Repeating a request that succeeded returns the same draft (`ALREADY_CREATED`).
- A definitive failure (validation, 4xx, missing resume) can be retried.
- A timeout, connection reset or Gmail 5xx returns 409 `EMAIL_DRAFT_OUTCOME_UNKNOWN`, because
the draft may already exist. It is never retried automatically. The user should check Gmail
drafts, and a new `idempotencyKey` is needed to create another copy.
- A concurrent identical request returns 409 `EMAIL_DRAFT_IN_PROGRESS`.
- Reusing a key with different content returns 409 `EMAIL_DRAFT_KEY_REUSED`.
- A failed draft never deletes or modifies the application or its resume.

## Observability

Metrics:

- `integrations.tinyfetch.requests{outcome}` and `integrations.tinyfetch.latency{outcome}`
- `integrations.gmail.drafts{outcome}` and `integrations.gmail.drafts.latency{outcome}`

Daily fetch and failure counts per user are stored in `tinyfetch_daily_usage`.

Logs record user id, host (never the full URL), outcome and latency. Recipients, subjects, bodies,
keys and tokens are never logged. MCP calls are also audited by the existing `@AuditMcpOperation`
aspect.
130 changes: 130 additions & 0 deletions src/main/java/com/jobtracker/config/IntegrationProperties.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package com.jobtracker.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.time.Duration;
import java.util.List;

/**
* Per-user external integrations (TinyFish Fetch, Gmail drafts). Secrets supplied by users are
* encrypted with {@link #encryptionKey} before they are persisted.
*/
@Component
@ConfigurationProperties(prefix = "app.integrations")
public class IntegrationProperties {

/** Base64-encoded 256-bit AES key. Without it, integrations that store secrets are unavailable. */
private String encryptionKey;
private final TinyFetch tinyfetch = new TinyFetch();
private final Gmail gmail = new Gmail();

public String getEncryptionKey() { return encryptionKey; }
public void setEncryptionKey(String encryptionKey) { this.encryptionKey = encryptionKey; }
public TinyFetch getTinyfetch() { return tinyfetch; }
public Gmail getGmail() { return gmail; }

public static class TinyFetch {
private String baseUrl = "https://api.fetch.tinyfish.ai";
private Duration connectTimeout = Duration.ofSeconds(5);
private Duration readTimeout = Duration.ofSeconds(45);
private int maxContentChars = 60_000;
private Duration cacheTtl = Duration.ofMinutes(15);
private int cacheMaxEntries = 500;
private int maxAttempts = 3;
private Duration retryBaseDelay = Duration.ofSeconds(1);
private Duration maxRetryDelay = Duration.ofSeconds(10);
/** Upper bounds from the TinyFish free tier; users may only configure lower values. */
private int maxPerMinuteLimit = 150;
private int maxDailyLimit = 1000;
private String testUrl = "https://example.com/";

public String getBaseUrl() { return baseUrl; }
public void setBaseUrl(String baseUrl) { this.baseUrl = baseUrl; }
public Duration getConnectTimeout() { return connectTimeout; }
public void setConnectTimeout(Duration connectTimeout) { this.connectTimeout = connectTimeout; }
public Duration getReadTimeout() { return readTimeout; }
public void setReadTimeout(Duration readTimeout) { this.readTimeout = readTimeout; }
public int getMaxContentChars() { return maxContentChars; }
public void setMaxContentChars(int maxContentChars) { this.maxContentChars = maxContentChars; }
public Duration getCacheTtl() { return cacheTtl; }
public void setCacheTtl(Duration cacheTtl) { this.cacheTtl = cacheTtl; }
public int getCacheMaxEntries() { return cacheMaxEntries; }
public void setCacheMaxEntries(int cacheMaxEntries) { this.cacheMaxEntries = cacheMaxEntries; }
public int getMaxAttempts() { return maxAttempts; }
public void setMaxAttempts(int maxAttempts) { this.maxAttempts = maxAttempts; }
public Duration getRetryBaseDelay() { return retryBaseDelay; }
public void setRetryBaseDelay(Duration retryBaseDelay) { this.retryBaseDelay = retryBaseDelay; }
public Duration getMaxRetryDelay() { return maxRetryDelay; }
public void setMaxRetryDelay(Duration maxRetryDelay) { this.maxRetryDelay = maxRetryDelay; }
public int getMaxPerMinuteLimit() { return maxPerMinuteLimit; }
public void setMaxPerMinuteLimit(int maxPerMinuteLimit) { this.maxPerMinuteLimit = maxPerMinuteLimit; }
public int getMaxDailyLimit() { return maxDailyLimit; }
public void setMaxDailyLimit(int maxDailyLimit) { this.maxDailyLimit = maxDailyLimit; }
public String getTestUrl() { return testUrl; }
public void setTestUrl(String testUrl) { this.testUrl = testUrl; }
}

public static class Gmail {
private String clientId;
private String clientSecret;
private String redirectUri;
private String oauthCompleteUrl;
private String authorizationUri = "https://accounts.google.com/o/oauth2/v2/auth";
private String tokenUri = "https://oauth2.googleapis.com/token";
private String revokeUri = "https://oauth2.googleapis.com/revoke";
private String apiBaseUrl = "https://gmail.googleapis.com/gmail/v1";
/** gmail.compose is the narrowest scope that allows drafts.create; it also covers users.getProfile. */
private List<String> scopes = List.of("https://www.googleapis.com/auth/gmail.compose");
private Duration connectTimeout = Duration.ofSeconds(5);
private Duration readTimeout = Duration.ofSeconds(30);
private Duration tokenRefreshSkew = Duration.ofSeconds(60);
private int maxAccountsPerUser = 5;
private int maxHtmlChars = 200_000;
/** Gmail caps a whole message at 25 MB after encoding; keep the raw attachment well under it. */
private int maxAttachmentBytes = 10 * 1024 * 1024;
/** A draft request stuck in PENDING longer than this is treated as ambiguous, never retried blindly. */
private Duration pendingTimeout = Duration.ofMinutes(2);

public boolean isConfigured() {
return hasText(clientId) && hasText(clientSecret) && hasText(redirectUri) && hasText(oauthCompleteUrl);
}

public String getClientId() { return clientId; }
public void setClientId(String clientId) { this.clientId = clientId; }
public String getClientSecret() { return clientSecret; }
public void setClientSecret(String clientSecret) { this.clientSecret = clientSecret; }
public String getRedirectUri() { return redirectUri; }
public void setRedirectUri(String redirectUri) { this.redirectUri = redirectUri; }
public String getOauthCompleteUrl() { return oauthCompleteUrl; }
public void setOauthCompleteUrl(String oauthCompleteUrl) { this.oauthCompleteUrl = oauthCompleteUrl; }
public String getAuthorizationUri() { return authorizationUri; }
public void setAuthorizationUri(String authorizationUri) { this.authorizationUri = authorizationUri; }
public String getTokenUri() { return tokenUri; }
public void setTokenUri(String tokenUri) { this.tokenUri = tokenUri; }
public String getRevokeUri() { return revokeUri; }
public void setRevokeUri(String revokeUri) { this.revokeUri = revokeUri; }
public String getApiBaseUrl() { return apiBaseUrl; }
public void setApiBaseUrl(String apiBaseUrl) { this.apiBaseUrl = apiBaseUrl; }
public List<String> getScopes() { return scopes; }
public void setScopes(List<String> scopes) { this.scopes = scopes; }
public Duration getConnectTimeout() { return connectTimeout; }
public void setConnectTimeout(Duration connectTimeout) { this.connectTimeout = connectTimeout; }
public Duration getReadTimeout() { return readTimeout; }
public void setReadTimeout(Duration readTimeout) { this.readTimeout = readTimeout; }
public Duration getTokenRefreshSkew() { return tokenRefreshSkew; }
public void setTokenRefreshSkew(Duration tokenRefreshSkew) { this.tokenRefreshSkew = tokenRefreshSkew; }
public int getMaxAccountsPerUser() { return maxAccountsPerUser; }
public void setMaxAccountsPerUser(int maxAccountsPerUser) { this.maxAccountsPerUser = maxAccountsPerUser; }
public int getMaxHtmlChars() { return maxHtmlChars; }
public void setMaxHtmlChars(int maxHtmlChars) { this.maxHtmlChars = maxHtmlChars; }
public int getMaxAttachmentBytes() { return maxAttachmentBytes; }
public void setMaxAttachmentBytes(int maxAttachmentBytes) { this.maxAttachmentBytes = maxAttachmentBytes; }
public Duration getPendingTimeout() { return pendingTimeout; }
public void setPendingTimeout(Duration pendingTimeout) { this.pendingTimeout = pendingTimeout; }

private static boolean hasText(String value) {
return value != null && !value.isBlank();
}
}
}
1 change: 1 addition & 0 deletions src/main/java/com/jobtracker/config/SecurityConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ public SecurityFilterChain securityFilterChain(
"/api/v1/auth/reset-password",
"/api/v1/auth/logout").permitAll()
.requestMatchers(HttpMethod.GET, "/api/v1/google-drive/oauth/callback").permitAll()
.requestMatchers(HttpMethod.GET, "/api/v1/integrations/gmail/oauth/callback").permitAll()
// Only this health group is served publicly on port 8080.
.requestMatchers(HttpMethod.GET, "/health").permitAll()
.requestMatchers("/v3/api-docs/**", "/swagger-ui/**", "/swagger-ui.html", "/v3/api-docs.yaml").permitAll()
Expand Down
Loading
Loading