@dev-zero/mcp-agent 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/zero-agent.mjs +5 -2
- package/package.json +2 -2
- package/src/browser-session-proof.mjs +121 -0
- package/src/chatgpt-session.mjs +12 -3
- package/src/first-run.mjs +1 -1
package/bin/zero-agent.mjs
CHANGED
|
@@ -104,9 +104,12 @@ async function start(){
|
|
|
104
104
|
console.log(' - ChatGPT session is not ready ('+reason+').');
|
|
105
105
|
if(reason==='ambiguous')
|
|
106
106
|
console.log(' - Multiple signed-in Chrome profiles found; use one ChatGPT account.');
|
|
107
|
-
else
|
|
107
|
+
else if(reason==='browser_busy'){
|
|
108
|
+
console.log(' - Close all Chrome windows for a few seconds so Zero can verify this signed-in profile locally.');
|
|
109
|
+
console.log(' - Zero will not kill Chrome or extract browser cookies.');
|
|
110
|
+
}else
|
|
108
111
|
console.log(' - Open Chrome, select or create your profile, and sign in at https://chatgpt.com/');
|
|
109
|
-
console.log(' - Waiting
|
|
112
|
+
console.log(' - Waiting with the same Device Code (no restart needed).');
|
|
110
113
|
},
|
|
111
114
|
onCode:({user_code,verification_uri_complete,expires_in})=>{
|
|
112
115
|
console.log(' - 🔌 Connected to Remote MCP');
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@dev-zero/mcp-agent",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"private": false,
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"start": "node bin/zero-agent.mjs remote",
|
|
16
16
|
"status": "node bin/zero-agent.mjs status",
|
|
17
17
|
"doctor": "node bin/zero-agent.mjs remote --help",
|
|
18
|
-
"test": "node --test tests/*.test.mjs",
|
|
18
|
+
"test": "node --test --test-concurrency=1 tests/*.test.mjs",
|
|
19
19
|
"packcheck": "npm pack --dry-run",
|
|
20
20
|
"release:eotp": "node scripts/release-eotp.mjs",
|
|
21
21
|
"release:eotp:check": "node scripts/release-eotp.mjs --check"
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import {existsSync,readFileSync,readdirSync} from 'node:fs';
|
|
2
|
+
import {homedir} from 'node:os';
|
|
3
|
+
import {join} from 'node:path';
|
|
4
|
+
import {spawnSync,execFileSync} from 'node:child_process';
|
|
5
|
+
|
|
6
|
+
const CHATGPT_SESSION='https://chatgpt.com/api/auth/session';
|
|
7
|
+
const PROFILE=/^(?:Default|Profile \d+)$/;
|
|
8
|
+
|
|
9
|
+
const chromeRoot=(env=process.env)=>env.ZERO_CHATGPT_CHROME_DATA_ROOT||
|
|
10
|
+
join(env.LOCALAPPDATA||join(homedir(),'AppData','Local'),'Google','Chrome','User Data');
|
|
11
|
+
|
|
12
|
+
const preferredProfile=(root)=>{
|
|
13
|
+
try{
|
|
14
|
+
const state=JSON.parse(readFileSync(join(root,'Local State'),'utf8'));
|
|
15
|
+
const name=state.profile?.last_used;
|
|
16
|
+
if(typeof name==='string'&&PROFILE.test(name))return name;
|
|
17
|
+
}catch{}
|
|
18
|
+
return 'Default';
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const profileCandidates=(root)=>{
|
|
22
|
+
const preferred=preferredProfile(root);
|
|
23
|
+
const others=[];
|
|
24
|
+
try{
|
|
25
|
+
for(const entry of readdirSync(root,{withFileTypes:true}))
|
|
26
|
+
if(entry.isDirectory()&&entry.name!==preferred&&PROFILE.test(entry.name))
|
|
27
|
+
others.push(entry.name);
|
|
28
|
+
}catch{}
|
|
29
|
+
return [preferred,...others.sort((a,b)=>a.localeCompare(b,undefined,{numeric:true}))];
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export function resolveChromeExecutable(env=process.env){
|
|
33
|
+
const custom=env.ZERO_CHATGPT_CHROME_EXE;
|
|
34
|
+
const candidates=[
|
|
35
|
+
custom,
|
|
36
|
+
env.PROGRAMFILES&&join(env.PROGRAMFILES,'Google','Chrome','Application','chrome.exe'),
|
|
37
|
+
env['PROGRAMFILES(X86)']&&join(env['PROGRAMFILES(X86)'],'Google','Chrome','Application','chrome.exe'),
|
|
38
|
+
env.LOCALAPPDATA&&join(env.LOCALAPPDATA,'Google','Chrome','Application','chrome.exe')
|
|
39
|
+
].filter(Boolean);
|
|
40
|
+
return candidates.find(existsSync)||null;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function isChromeRunning({run=execFileSync}={}){
|
|
44
|
+
if(process.platform!=='win32')return false;
|
|
45
|
+
try{
|
|
46
|
+
const output=run('tasklist.exe',['/FI','IMAGENAME eq chrome.exe','/NH'],{
|
|
47
|
+
encoding:'utf8',windowsHide:true,timeout:3000,stdio:['ignore','pipe','ignore']
|
|
48
|
+
});
|
|
49
|
+
return /(^|\r?\n)chrome\.exe\s/i.test(String(output));
|
|
50
|
+
}catch{return false}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
const decodeEntities=value=>String(value)
|
|
54
|
+
.replace(/"/g,'"').replace(/'|'/g,"'")
|
|
55
|
+
.replace(/</g,'<').replace(/>/g,'>').replace(/&/g,'&');
|
|
56
|
+
|
|
57
|
+
const authenticatedDump=stdout=>{
|
|
58
|
+
const raw=Buffer.isBuffer(stdout)?stdout.toString('utf8'):String(stdout||'');
|
|
59
|
+
const match=/<pre[^>]*>([\s\S]*?)<\/pre>/i.exec(raw)||
|
|
60
|
+
/<body[^>]*>([\s\S]*?)<\/body>/i.exec(raw);
|
|
61
|
+
if(!match)return false;
|
|
62
|
+
const text=decodeEntities(match[1].replace(/<[^>]+>/g,'')).trim();
|
|
63
|
+
let body;
|
|
64
|
+
try{body=JSON.parse(text)}catch{return false}
|
|
65
|
+
return Boolean(body&&typeof body==='object'&&!Array.isArray(body)&&
|
|
66
|
+
(body.user||body.account||body.accessToken||body.sessionToken));
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
export function probeChromeProfile({chrome,root,profile,run=spawnSync}){
|
|
70
|
+
if(!chrome||!existsSync(root)||!PROFILE.test(profile))return false;
|
|
71
|
+
// This runs only when no Chrome process owns the real profile. Chrome itself
|
|
72
|
+
// decrypts/uses its cookies; Zero never extracts or logs them. stdout stays
|
|
73
|
+
// in memory and is reduced to a boolean authenticated/not-authenticated.
|
|
74
|
+
const args=[
|
|
75
|
+
'--headless=new','--disable-gpu','--no-first-run','--no-default-browser-check',
|
|
76
|
+
'--disable-background-networking','--disable-component-update','--disable-sync',
|
|
77
|
+
'--disable-default-apps','--disable-features=MediaRouter',
|
|
78
|
+
'--virtual-time-budget=7000','--user-data-dir='+root,'--profile-directory='+profile,
|
|
79
|
+
'--dump-dom',CHATGPT_SESSION
|
|
80
|
+
];
|
|
81
|
+
try{
|
|
82
|
+
const result=run(chrome,args,{
|
|
83
|
+
windowsHide:true,encoding:null,timeout:20000,maxBuffer:2*1024*1024,
|
|
84
|
+
stdio:['ignore','pipe','ignore']
|
|
85
|
+
});
|
|
86
|
+
return !result?.error&&authenticatedDump(result?.stdout);
|
|
87
|
+
}catch{return false}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export async function verifyChromeBrowserSession({
|
|
91
|
+
env=process.env,browserRunning=isChromeRunning,probeProfile=probeChromeProfile
|
|
92
|
+
}={}){
|
|
93
|
+
const root=chromeRoot(env);
|
|
94
|
+
if(!existsSync(root))
|
|
95
|
+
return {state:'auth_required',runtime:'ready',auth:'missing',redacted:true};
|
|
96
|
+
if(browserRunning())
|
|
97
|
+
return {state:'auth_required',runtime:'ready',auth:'browser_busy',redacted:true};
|
|
98
|
+
const chrome=resolveChromeExecutable(env);
|
|
99
|
+
if(!chrome)
|
|
100
|
+
return {state:'auth_required',runtime:'ready',auth:'missing',redacted:true};
|
|
101
|
+
|
|
102
|
+
const profiles=profileCandidates(root);
|
|
103
|
+
if(profiles.length===0)
|
|
104
|
+
return {state:'auth_required',runtime:'ready',auth:'missing',redacted:true};
|
|
105
|
+
|
|
106
|
+
const preferred=profiles[0];
|
|
107
|
+
if(await probeProfile({chrome,root,profile:preferred}))
|
|
108
|
+
return {state:'auth_valid',runtime:'ready',auth:'valid',redacted:true};
|
|
109
|
+
|
|
110
|
+
let found=0;
|
|
111
|
+
for(const profile of profiles.slice(1)){
|
|
112
|
+
if(await probeProfile({chrome,root,profile})){
|
|
113
|
+
found++;
|
|
114
|
+
if(found>1)
|
|
115
|
+
return {state:'auth_required',runtime:'ready',auth:'ambiguous',redacted:true};
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return found===1
|
|
119
|
+
?{state:'auth_valid',runtime:'ready',auth:'valid',redacted:true}
|
|
120
|
+
:{state:'auth_required',runtime:'ready',auth:'invalid',redacted:true};
|
|
121
|
+
}
|
package/src/chatgpt-session.mjs
CHANGED
|
@@ -3,6 +3,7 @@ import https from 'node:https';
|
|
|
3
3
|
import { mkdtempSync,existsSync,readdirSync,readFileSync,copyFileSync,mkdirSync,rmSync } from 'node:fs';
|
|
4
4
|
import { tmpdir,homedir } from 'node:os';
|
|
5
5
|
import { join } from 'node:path';
|
|
6
|
+
import {verifyChromeBrowserSession} from './browser-session-proof.mjs';
|
|
6
7
|
|
|
7
8
|
const CHATGPT_SESSION='https://chatgpt.com/api/auth/session';
|
|
8
9
|
const SESSION_STORAGE_KEY='oai/apps/accountSwitchSessions';
|
|
@@ -123,10 +124,18 @@ function directRequest(url,options) {
|
|
|
123
124
|
});
|
|
124
125
|
}
|
|
125
126
|
|
|
126
|
-
export async function ensureLocalChatGPTSession({env=process.env,request=directRequest,findMarker}={}){
|
|
127
|
+
export async function ensureLocalChatGPTSession({env=process.env,request=directRequest,findMarker,browserProof=verifyChromeBrowserSession}={}){
|
|
127
128
|
const found=await findLocalChatGPTSession({env,...(findMarker?{findMarker}:{})});
|
|
128
|
-
if(!found.marker)
|
|
129
|
-
|
|
129
|
+
if(!found.marker){
|
|
130
|
+
if(found.auth==='ambiguous')return {state:'auth_required',runtime:'ready',auth:'ambiguous',redacted:true};
|
|
131
|
+
const proof=await browserProof({env});
|
|
132
|
+
if(proof?.state==='auth_valid'&&proof.auth==='valid'&&proof.redacted===true)
|
|
133
|
+
return {state:'auth_valid',runtime:'ready',auth:'valid',redacted:true};
|
|
134
|
+
if(proof?.state==='auth_required'&&proof.redacted===true&&
|
|
135
|
+
['missing','expired','invalid','ambiguous','browser_busy'].includes(proof.auth))
|
|
136
|
+
return {state:'auth_required',runtime:'ready',auth:proof.auth,redacted:true};
|
|
137
|
+
throw new Error('CHATGPT_BROWSER_PROOF_INVALID');
|
|
138
|
+
}
|
|
130
139
|
// The marker stays in memory and is sent only to chatgpt.com over HTTPS.
|
|
131
140
|
// No token is included in returned Agent status, logs or persisted state.
|
|
132
141
|
let cookie='__Secure-next-auth.session-token='+encodeURIComponent(found.marker);
|
package/src/first-run.mjs
CHANGED
|
@@ -79,7 +79,7 @@ export async function bootstrapAgent({gatewayUrl,vaultFile,ensure=ensureLocalCha
|
|
|
79
79
|
// a positive proof, register an Agent, or switch Chrome profiles here.
|
|
80
80
|
const reason=local?.auth;
|
|
81
81
|
if(local?.state!=='auth_required'||local?.redacted!==true||
|
|
82
|
-
!['missing','expired','invalid','ambiguous'].includes(reason))
|
|
82
|
+
!['missing','expired','invalid','ambiguous','browser_busy'].includes(reason))
|
|
83
83
|
throw new Error('CHATGPT_LOCAL_SESSION_REQUIRED');
|
|
84
84
|
if(!reportedAuthReasons.has(reason)){
|
|
85
85
|
reportedAuthReasons.add(reason);
|