Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
d1fcbb6
feat(external-context): Support open-source Mem0 providers
yiliang114 Aug 24, 2026
b4c1839
test(external-context): Cover the open-source Mem0 example config
yiliang114 Aug 24, 2026
ee3fefb
refactor(external-context): Address review on the mem0 provider
yiliang114 Aug 24, 2026
04aaef5
fix(external-context): Ship the mem0 example with HTTPS defaults
yiliang114 Aug 24, 2026
ec2707f
test(external-context): prove shipped mem0 example constructs a provider
yiliang114 Aug 25, 2026
d729081
refactor(external-context): address mem0 review round — merge duplica…
yiliang114 Aug 25, 2026
5702f63
fix(external-context): Scope OSS mem0 by agent_id instead of app_id
yiliang114 Aug 25, 2026
a381a6f
fix(external-context): Scope Mem0 provider to PolarDB
yiliang114 Aug 25, 2026
184071d
feat(external-context): add versioned Mem0 presets
yiliang114 Aug 27, 2026
c02db5b
fix(external-context): survive an unverified Mem0 deployment build
yiliang114 Aug 27, 2026
b1ded13
feat(external-context): add a read-only Mem0 preset probe
yiliang114 Aug 27, 2026
52892f7
refactor(external-context): drop the vendor prefix from the PolarDB p…
yiliang114 Aug 27, 2026
657c3f5
fix(external-context): restore bounded Mem0 contracts
yiliang114 Aug 27, 2026
eb21ca1
docs(external-context): ship a PolarDB example that loads as it is sh…
yiliang114 Aug 27, 2026
68cd9a0
docs(external-context): clarify PolarDB setup
yiliang114 Aug 27, 2026
1ebda66
Merge branch 'main' into feat/external-context-mem0-provider
yiliang114 Aug 30, 2026
de5531a
Merge branch 'main' into feat/external-context-mem0-provider
yiliang114 Sep 1, 2026
50c7b2d
Merge branch 'main' into feat/external-context-mem0-provider
yiliang114 Sep 2, 2026
155b4c0
Merge branch 'main' into feat/external-context-mem0-provider
yiliang114 Sep 2, 2026
9ea2b62
Merge branch 'main' into feat/external-context-mem0-provider
yiliang114 Sep 3, 2026
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
20 changes: 20 additions & 0 deletions integrations/external-context/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,26 @@ it disabled when search must have no semantic provider-side state change.
Provider audit or access logs may still be retained. See
[Mem0 Memory Decay](https://docs.mem0.ai/platform/features/memory-decay).

## Mem0 (open-source protocol)
Comment thread
yiliang114 marked this conversation as resolved.
Resolved

The `mem0` provider type targets services that expose the open-source Mem0
REST API, such as a self-hosted mem0 server or Aliyun PolarDB Mem0. Search
calls `POST {baseUrl}/v2/memories/search` with the configured `user_id` (and
optional `app_id`) under `filters`; writes call `POST {baseUrl}/v1/memories`
with one exact user message and `infer: false`. Both paths are fixed by the
open-source Mem0 protocol; the `baseUrl` (scheme, host, and port only) is the
operator's switch for choosing the vendor — moving from one managed mem0
service to another is a config change, not a code change.

These services commonly expose plain HTTP on an IP whitelist. Because the
credential travels in cleartext on HTTP, keep `allowInsecureHttp` off unless
the whitelist is the intended network control, and prefer HTTPS or a loopback
relay (for example `ssh -L` or a VPC-side proxy) when one is available.

`userId` selects the tenant whose memories are read and written. It is fixed
in the configuration and is never supplied by the model. See
`examples/mem0-oss.json` for a complete configuration.
Comment thread
yiliang114 marked this conversation as resolved.
Resolved

## Rollout and rollback

Start with the pinned read-only on-demand MCP for one workspace and validate
Expand Down
12 changes: 12 additions & 0 deletions integrations/external-context/examples/mem0-oss.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"version": 1,
"timeoutMs": 5000,
"provider": {
"type": "mem0",
"baseUrl": "http://<your-mem0-endpoint>:8080",
"apiKeyEnv": "MEM0_API_KEY",
"userId": "<your-user-id>",
"appId": "qwen-code",
"allowInsecureHttp": true
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
}
}
1 change: 1 addition & 0 deletions integrations/external-context/src/auto-recall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,7 @@ function isWithin(root: string, candidate: string): boolean {
function providerCredential(config: ProviderConfig): string {
switch (config.type) {
case 'mem0-platform-v3':
case 'mem0':
return config.apiKey;
case 'generic-http-search-v1':
return config.token;
Expand Down
123 changes: 123 additions & 0 deletions integrations/external-context/src/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,129 @@ describe('loadConfig', () => {
});
});

describe('mem0 OSS provider', () => {
const provider = {
type: 'mem0',
baseUrl: 'https://mem0.example.com:8443',
apiKeyEnv: 'MEM0_API_KEY',
userId: 'fixed-user',
};

it('resolves the credential and fixed user binding', async () => {
const fixture = await createFixture();
await writeConfig(fixture, { version: 1, provider });

await expect(
loadConfig({
QWEN_EXTERNAL_CONTEXT_CONFIG: fixture.config,
MEM0_API_KEY: 'secret-value',
}),
).resolves.toMatchObject({
version: 1,
provider: {
type: 'mem0',
baseUrl: 'https://mem0.example.com:8443',
apiKey: 'secret-value',
userId: 'fixed-user',
},
});
});

it('enables writes for a v1 mem0 provider', async () => {
const fixture = await createFixture();
await writeConfig(fixture, {
version: 1,
write: { enabled: true },
provider,
});

await expect(
loadConfig({
QWEN_EXTERNAL_CONTEXT_CONFIG: fixture.config,
MEM0_API_KEY: 'secret-value',
}),
).resolves.toMatchObject({
version: 1,
write: { enabled: true },
provider: { type: 'mem0' },
});
});

it('accepts a plain-HTTP baseUrl only with allowInsecureHttp', async () => {
const fixture = await createFixture();
await writeConfig(fixture, {
version: 1,
provider: {
...provider,
baseUrl: 'http://192.0.2.1:8080',
allowInsecureHttp: true,
},
});

await expect(
loadConfig({
QWEN_EXTERNAL_CONTEXT_CONFIG: fixture.config,
MEM0_API_KEY: 'secret-value',
}),
).resolves.toMatchObject({
provider: { type: 'mem0', allowInsecureHttp: true },
});

const rejected = await createFixture();
await writeConfig(rejected, {
version: 1,
provider: { ...provider, baseUrl: 'http://192.0.2.1:8080' },
});

await expect(
loadConfig({
QWEN_EXTERNAL_CONTEXT_CONFIG: rejected.config,
MEM0_API_KEY: 'secret-value',
}),
).rejects.toThrow('Provider URL must use HTTPS or loopback HTTP.');
});

it('rejects a credentialed baseUrl even with allowInsecureHttp', async () => {
const fixture = await createFixture();
await writeConfig(fixture, {
version: 1,
provider: {
...provider,
baseUrl: 'http://key@192.0.2.1:8080',
allowInsecureHttp: true,
},
});

await expect(
loadConfig({
QWEN_EXTERNAL_CONTEXT_CONFIG: fixture.config,
MEM0_API_KEY: 'secret-value',
}),
).rejects.toThrow(
'Provider URL must not contain credentials, path, query, or fragment.',
);
});

it('rejects a mem0 provider without userId', async () => {
const fixture = await createFixture();
await writeConfig(fixture, {
version: 1,
provider: {
type: 'mem0',
baseUrl: 'https://mem0.example.com:8443',
apiKeyEnv: 'MEM0_API_KEY',
},
});

await expect(
loadConfig({
QWEN_EXTERNAL_CONTEXT_CONFIG: fixture.config,
MEM0_API_KEY: 'secret-value',
}),
).rejects.toThrow('External context config is invalid.');
});
});

it('rejects writes for Generic HTTP and v2 configurations', async () => {
const fixture = await createFixture();
await writeConfig(fixture, {
Expand Down
30 changes: 28 additions & 2 deletions integrations/external-context/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
import { readFile, realpath, stat } from 'node:fs/promises';
import { isAbsolute, parse } from 'node:path';
import { z } from 'zod';
import { validateProviderBaseUrl } from './http-client.js';
import type {
ExternalContextConfig,
GenericHttpProviderConfig,
Mem0OssProviderConfig,
Mem0ProviderConfig,
} from './types.js';

Expand All @@ -24,6 +26,16 @@ const providerSchema = z.discriminatedUnion('type', [
appId: z.string().trim().min(1).max(256),
})
.strict(),
z
.object({
type: z.literal('mem0'),
baseUrl: z.string().url(),
apiKeyEnv: z.string().regex(ENV_NAME),
userId: z.string().trim().min(1).max(256),
appId: z.string().trim().min(1).max(256).optional(),
allowInsecureHttp: z.boolean().optional(),
})
.strict(),
z
.object({
type: z.literal('generic-http-search-v1'),
Expand Down Expand Up @@ -114,7 +126,8 @@ export async function loadConfig(
if (result.data.version === 1) {
if (
result.data.write !== undefined &&
result.data.provider.type !== 'mem0-platform-v3'
result.data.provider.type !== 'mem0-platform-v3' &&
result.data.provider.type !== 'mem0'
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
) {
throw new ConfigurationError(
'External context memory writes require a Mem0 provider.',
Expand Down Expand Up @@ -146,12 +159,25 @@ export async function loadConfig(
function resolveProvider(
provider: z.infer<typeof providerSchema>,
env: NodeJS.ProcessEnv,
): Mem0ProviderConfig | GenericHttpProviderConfig {
): Mem0ProviderConfig | Mem0OssProviderConfig | GenericHttpProviderConfig {
switch (provider.type) {
case 'mem0-platform-v3': {
const apiKey = readCredential(env, provider.apiKeyEnv);
return { ...provider, apiKey };
}
case 'mem0': {
const apiKey = readCredential(env, provider.apiKeyEnv);
try {
validateProviderBaseUrl(provider.baseUrl, {
allowInsecureHttp: provider.allowInsecureHttp ?? false,
});
} catch (error) {
throw new ConfigurationError(
error instanceof Error ? error.message : String(error),
);
}
return { ...provider, apiKey };
}
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
case 'generic-http-search-v1': {
const token = readCredential(env, provider.tokenEnv);
return { ...provider, token };
Expand Down
41 changes: 40 additions & 1 deletion integrations/external-context/src/http-client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@
*/

import { afterEach, describe, expect, it, vi } from 'vitest';
import { MAX_RESPONSE_BYTES, postJson } from './http-client.js';
import {
MAX_RESPONSE_BYTES,
postJson,
validateProviderBaseUrl,
} from './http-client.js';

// Behavioral net for readBoundedBody's reader loop. The loop was rewritten
// from `for await (const chunk of response.body)` to an explicit getReader()
Expand Down Expand Up @@ -220,3 +224,38 @@ describe('postJson bounded body reading', () => {
);
});
});

describe('validateProviderBaseUrl', () => {
it('accepts HTTPS and loopback HTTP by default', () => {
expect(validateProviderBaseUrl('https://mem0.example.com').protocol).toBe(
'https:',
);
expect(validateProviderBaseUrl('http://127.0.0.1:8080').hostname).toBe(
'127.0.0.1',
);
});
Comment thread
yiliang114 marked this conversation as resolved.
Resolved

it('rejects non-loopback HTTP unless explicitly allowed', () => {
expect(() => validateProviderBaseUrl('http://192.0.2.1:8080')).toThrow(
'Provider URL must use HTTPS or loopback HTTP.',
);
expect(
validateProviderBaseUrl('http://192.0.2.1:8080', {
allowInsecureHttp: true,
}).hostname,
).toBe('192.0.2.1');
});

it('still rejects credentialed and pathed URLs when HTTP is allowed', () => {
expect(() =>
validateProviderBaseUrl('http://key@192.0.2.1:8080', {
allowInsecureHttp: true,
}),
).toThrow('Provider URL must not contain credentials');
expect(() =>
validateProviderBaseUrl('http://192.0.2.1:8080/prefix', {
allowInsecureHttp: true,
}),
).toThrow('Provider URL must not contain credentials, path');
});
});
19 changes: 10 additions & 9 deletions integrations/external-context/src/http-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ export class ProviderHttpStatusError extends Error {
}
}

export function validateProviderBaseUrl(value: string): URL {
export function validateProviderBaseUrl(
value: string,
options?: { allowInsecureHttp?: boolean },
): URL {
let url: URL;
try {
url = new URL(value);
Expand All @@ -38,20 +41,18 @@ export function validateProviderBaseUrl(value: string): URL {
'Provider URL must not contain credentials, path, query, or fragment.',
);
}
if (url.protocol === 'https:') {
return url;
}
if (
url.protocol === 'http:' &&
(url.hostname === 'localhost' ||
url.hostname === '127.0.0.1' ||
url.hostname === '[::1]')
url.protocol === 'https:' ||
(url.protocol === 'http:' &&
(url.hostname === 'localhost' ||
url.hostname === '127.0.0.1' ||
url.hostname === '[::1]' ||
options?.allowInsecureHttp === true))
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
) {
return url;
}
throw new Error('Provider URL must use HTTPS or loopback HTTP.');
}

export async function postJson(input: {
url: URL;
authorization: string;
Expand Down
45 changes: 44 additions & 1 deletion integrations/external-context/src/manifest.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { readFile } from 'node:fs/promises';
import { mkdtemp, readFile, writeFile } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { describe, expect, it } from 'vitest';
import { loadConfig } from './config.js';

describe('extension manifest', () => {
it('exposes only the retrieval tool', async () => {
Expand Down Expand Up @@ -233,6 +236,46 @@ describe('extension manifest', () => {
},
});
});
it('keeps the open-source Mem0 example loadable after placeholder substitution', async () => {
const config = await readJson('../examples/mem0-oss.json');

expect(config).toEqual({
version: 1,
timeoutMs: 5000,
provider: {
type: 'mem0',
baseUrl: 'http://<your-mem0-endpoint>:8080',
apiKeyEnv: 'MEM0_API_KEY',
userId: '<your-user-id>',
appId: 'qwen-code',
allowInsecureHttp: true,
},
});

const substituted = JSON.parse(
JSON.stringify(config)
.replaceAll('<your-mem0-endpoint>', 'mem0.example.internal')
.replaceAll('<your-user-id>', 'example-user'),
);
const directory = await mkdtemp(
join(tmpdir(), 'external-context-example-'),
);
Comment thread
yiliang114 marked this conversation as resolved.
Resolved
const configPath = join(directory, 'config.json');
await writeFile(configPath, JSON.stringify(substituted));

await expect(
loadConfig({
QWEN_EXTERNAL_CONTEXT_CONFIG: configPath,
MEM0_API_KEY: 'example-key',
}),
).resolves.toMatchObject({
provider: {
type: 'mem0',
userId: 'example-user',
appId: 'qwen-code',
},
});
});

it('pins write confirmation and disables local persistence', async () => {
const settings = await readJson(
Expand Down
Loading
Loading