@openshain/mcp 0.1.0
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/LICENSE +202 -0
- package/README.md +7 -0
- package/package.json +45 -0
- package/src/index.ts +3 -0
- package/src/server.ts +348 -0
- package/src/session.ts +26 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
202
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# @openshain/mcp
|
|
2
|
+
|
|
3
|
+
MCP server that exposes an openshain workspace to Claude Code and other agents.
|
|
4
|
+
|
|
5
|
+
Part of [openshain](https://github.com/openshain/openshain). Requires Bun 1.3 or later: the package ships TypeScript source. Documentation, in Japanese, lives in the repository.
|
|
6
|
+
|
|
7
|
+
License: Apache-2.0
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@openshain/mcp",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "MCP server that exposes an openshain workspace to any agent",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"openshain",
|
|
7
|
+
"agent",
|
|
8
|
+
"runtime",
|
|
9
|
+
"mcp",
|
|
10
|
+
"accounting",
|
|
11
|
+
"japan"
|
|
12
|
+
],
|
|
13
|
+
"author": "Hiroki Nakatani",
|
|
14
|
+
"license": "Apache-2.0",
|
|
15
|
+
"homepage": "https://github.com/openshain/openshain",
|
|
16
|
+
"repository": {
|
|
17
|
+
"type": "git",
|
|
18
|
+
"url": "git+https://github.com/openshain/openshain.git",
|
|
19
|
+
"directory": "packages/mcp"
|
|
20
|
+
},
|
|
21
|
+
"bugs": "https://github.com/openshain/openshain/issues",
|
|
22
|
+
"type": "module",
|
|
23
|
+
"engines": {
|
|
24
|
+
"bun": ">=1.3"
|
|
25
|
+
},
|
|
26
|
+
"files": [
|
|
27
|
+
"src",
|
|
28
|
+
"!src/**/*.test.ts",
|
|
29
|
+
"README.md",
|
|
30
|
+
"LICENSE"
|
|
31
|
+
],
|
|
32
|
+
"exports": {
|
|
33
|
+
".": "./src/index.ts"
|
|
34
|
+
},
|
|
35
|
+
"publishConfig": {
|
|
36
|
+
"access": "public"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"@modelcontextprotocol/sdk": "1.30.0",
|
|
40
|
+
"@openshain/core": "0.1.0"
|
|
41
|
+
},
|
|
42
|
+
"devDependencies": {
|
|
43
|
+
"@openshain/tools": "0.1.0"
|
|
44
|
+
}
|
|
45
|
+
}
|
package/src/index.ts
ADDED
package/src/server.ts
ADDED
|
@@ -0,0 +1,348 @@
|
|
|
1
|
+
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
|
|
2
|
+
import {
|
|
3
|
+
CallToolRequestSchema,
|
|
4
|
+
type CallToolResult,
|
|
5
|
+
ListToolsRequestSchema,
|
|
6
|
+
type Tool,
|
|
7
|
+
} from "@modelcontextprotocol/sdk/types.js";
|
|
8
|
+
import {
|
|
9
|
+
type AnyEvent,
|
|
10
|
+
type Artifact,
|
|
11
|
+
compileInputValidator,
|
|
12
|
+
createToolCaller,
|
|
13
|
+
createToolRegistry,
|
|
14
|
+
type Event,
|
|
15
|
+
type InputValidation,
|
|
16
|
+
isOpenshainError,
|
|
17
|
+
isTerminal,
|
|
18
|
+
loadConfig,
|
|
19
|
+
parseWorkId,
|
|
20
|
+
type RuntimeProviders,
|
|
21
|
+
resolveWorkspacePath,
|
|
22
|
+
SESSION_WORK_TYPE,
|
|
23
|
+
type ToolDefinition,
|
|
24
|
+
type ToolResult,
|
|
25
|
+
verifyArtifact,
|
|
26
|
+
type Work,
|
|
27
|
+
type WorkId,
|
|
28
|
+
WorkStore,
|
|
29
|
+
} from "@openshain/core";
|
|
30
|
+
import { Session } from "./session.ts";
|
|
31
|
+
|
|
32
|
+
export interface McpServerOptions {
|
|
33
|
+
workspaceRoot: string;
|
|
34
|
+
/** Tool providers by the id used in openshain.yaml. */
|
|
35
|
+
tools: RuntimeProviders["tools"];
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** The tools every session has, before the workspace's own. Their names are reserved in the runtime. */
|
|
39
|
+
const WORK_TOOLS: Tool[] = [
|
|
40
|
+
{
|
|
41
|
+
name: "work_create",
|
|
42
|
+
description:
|
|
43
|
+
"Start a work for a request from the person you work for, and make it the current work. Tool calls are recorded against the current work. Finish the current work with work_complete or work_fail before starting another.",
|
|
44
|
+
inputSchema: {
|
|
45
|
+
type: "object",
|
|
46
|
+
properties: {
|
|
47
|
+
objective: { type: "string", description: "The request, in the person's words." },
|
|
48
|
+
type: {
|
|
49
|
+
type: "string",
|
|
50
|
+
description: "A short label for the kind of work. Defaults to request.",
|
|
51
|
+
},
|
|
52
|
+
},
|
|
53
|
+
required: ["objective"],
|
|
54
|
+
additionalProperties: false,
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
name: "work_select",
|
|
59
|
+
description: "Make an existing work the current one. A finished work cannot be selected.",
|
|
60
|
+
inputSchema: {
|
|
61
|
+
type: "object",
|
|
62
|
+
properties: { id: { type: "string" } },
|
|
63
|
+
required: ["id"],
|
|
64
|
+
additionalProperties: false,
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
name: "work_get",
|
|
69
|
+
description: "The state of the current work, or of the work with the given id.",
|
|
70
|
+
inputSchema: {
|
|
71
|
+
type: "object",
|
|
72
|
+
properties: { id: { type: "string" } },
|
|
73
|
+
additionalProperties: false,
|
|
74
|
+
},
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
name: "work_list",
|
|
78
|
+
description: "Every work in this workspace, oldest first, with the ones that cannot be read.",
|
|
79
|
+
inputSchema: { type: "object", properties: {}, additionalProperties: false },
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
name: "work_complete",
|
|
83
|
+
description:
|
|
84
|
+
"Finish the current work. Say what was done; name the files you produced with their sha256 if you know it. Paths are relative to the workspace. The runtime checks every file and records its own hash.",
|
|
85
|
+
inputSchema: {
|
|
86
|
+
type: "object",
|
|
87
|
+
properties: {
|
|
88
|
+
summary: { type: "string" },
|
|
89
|
+
artifacts: {
|
|
90
|
+
type: "array",
|
|
91
|
+
items: {
|
|
92
|
+
type: "object",
|
|
93
|
+
properties: { path: { type: "string" }, sha256: { type: "string" } },
|
|
94
|
+
required: ["path"],
|
|
95
|
+
additionalProperties: false,
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
},
|
|
99
|
+
required: ["summary"],
|
|
100
|
+
additionalProperties: false,
|
|
101
|
+
},
|
|
102
|
+
},
|
|
103
|
+
{
|
|
104
|
+
name: "work_fail",
|
|
105
|
+
description: "Give up on the current work. Say why in a short reason and, if useful, a detail.",
|
|
106
|
+
inputSchema: {
|
|
107
|
+
type: "object",
|
|
108
|
+
properties: { reason: { type: "string" }, detail: { type: "string" } },
|
|
109
|
+
required: ["reason"],
|
|
110
|
+
additionalProperties: false,
|
|
111
|
+
},
|
|
112
|
+
},
|
|
113
|
+
];
|
|
114
|
+
|
|
115
|
+
const NO_WORK =
|
|
116
|
+
"no current work: call work_create to start one for the person's request, or work_select to pick an existing one";
|
|
117
|
+
|
|
118
|
+
const validators = new Map(
|
|
119
|
+
WORK_TOOLS.map((tool) => [tool.name, compileInputValidator(tool.inputSchema)] as const),
|
|
120
|
+
);
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* An MCP server over one workspace. The agent on the other side thinks; the server keeps the
|
|
124
|
+
* work's state, runs the workspace's tools and records everything. Needs no model provider.
|
|
125
|
+
* Calls are handled one at a time per connection.
|
|
126
|
+
*/
|
|
127
|
+
export async function createMcpServer(options: McpServerOptions): Promise<Server> {
|
|
128
|
+
const { workspaceRoot } = options;
|
|
129
|
+
const config = await loadConfig(workspaceRoot);
|
|
130
|
+
const registry = await createToolRegistry(workspaceRoot, config, options.tools);
|
|
131
|
+
const callTool = createToolCaller({ registry, config, workspaceRoot });
|
|
132
|
+
const works = new WorkStore(workspaceRoot);
|
|
133
|
+
const session = new Session();
|
|
134
|
+
const server = new Server(
|
|
135
|
+
{ name: "openshain", version: "0.0.0" },
|
|
136
|
+
{ capabilities: { tools: {} } },
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
/** The current work when it can still take events; otherwise the reason it cannot. */
|
|
140
|
+
async function openWork(): Promise<{ id: WorkId } | { refused: CallToolResult }> {
|
|
141
|
+
const id = session.current;
|
|
142
|
+
if (!id) return { refused: failure(NO_WORK) };
|
|
143
|
+
const work = await works.get(id);
|
|
144
|
+
if (isTerminal(work.status)) {
|
|
145
|
+
session.clear();
|
|
146
|
+
return { refused: failure(`work ${id} is already ${work.status}; ${NO_WORK}`) };
|
|
147
|
+
}
|
|
148
|
+
return { id };
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async function handle(name: string, input: Record<string, unknown>): Promise<CallToolResult> {
|
|
152
|
+
const validate = validators.get(name);
|
|
153
|
+
if (validate) {
|
|
154
|
+
const checked: InputValidation = validate(input);
|
|
155
|
+
if (!checked.ok) {
|
|
156
|
+
return failure(
|
|
157
|
+
`schema_mismatch: input does not match the schema of ${name}: ${checked.reason}`,
|
|
158
|
+
);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
switch (name) {
|
|
162
|
+
case "work_create": {
|
|
163
|
+
const current = session.current;
|
|
164
|
+
if (current && !isTerminal((await works.get(current)).status)) {
|
|
165
|
+
return failure(
|
|
166
|
+
`work ${current} is still in progress; finish it with work_complete or work_fail before starting another`,
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
const { objective, type } = input as { objective: string; type?: string };
|
|
170
|
+
if (type === SESSION_WORK_TYPE) {
|
|
171
|
+
return failure(
|
|
172
|
+
`type "${SESSION_WORK_TYPE}" is reserved for conversations; use another label, such as request`,
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
const work = await works.create({
|
|
176
|
+
objective,
|
|
177
|
+
principal: config.principal.id,
|
|
178
|
+
profession: config.profession.id,
|
|
179
|
+
...(type && { type }),
|
|
180
|
+
});
|
|
181
|
+
await works.transition(work.id, "in_progress", "an agent took the work over MCP");
|
|
182
|
+
session.select(work.id);
|
|
183
|
+
return json(await works.get(work.id));
|
|
184
|
+
}
|
|
185
|
+
case "work_select": {
|
|
186
|
+
const id = parseWorkId((input as { id: string }).id);
|
|
187
|
+
const work = await works.get(id);
|
|
188
|
+
if (isTerminal(work.status)) return failure(`work ${id} is already ${work.status}`);
|
|
189
|
+
session.select(id);
|
|
190
|
+
return json(work);
|
|
191
|
+
}
|
|
192
|
+
case "work_get": {
|
|
193
|
+
const given = (input as { id?: string }).id;
|
|
194
|
+
const id = given ? parseWorkId(given) : session.current;
|
|
195
|
+
if (!id) return failure(NO_WORK);
|
|
196
|
+
return json(await works.get(id));
|
|
197
|
+
}
|
|
198
|
+
case "work_list": {
|
|
199
|
+
const { works: all, problems } = await works.list();
|
|
200
|
+
return json({
|
|
201
|
+
works: all.map((w) => ({
|
|
202
|
+
id: w.id,
|
|
203
|
+
status: w.status,
|
|
204
|
+
objective: w.objective,
|
|
205
|
+
createdAt: w.createdAt,
|
|
206
|
+
})),
|
|
207
|
+
problems: problems.map((p) => ({
|
|
208
|
+
id: p.id,
|
|
209
|
+
code: p.error.code,
|
|
210
|
+
message: p.error.message,
|
|
211
|
+
})),
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
case "work_complete": {
|
|
215
|
+
const gate = await openWork();
|
|
216
|
+
if ("refused" in gate) return gate.refused;
|
|
217
|
+
const { summary, artifacts } = input as {
|
|
218
|
+
summary: string;
|
|
219
|
+
artifacts?: { path: string; sha256?: string }[];
|
|
220
|
+
};
|
|
221
|
+
const work = await complete(works, workspaceRoot, gate.id, summary, artifacts ?? []);
|
|
222
|
+
session.clear();
|
|
223
|
+
return json(work);
|
|
224
|
+
}
|
|
225
|
+
case "work_fail": {
|
|
226
|
+
const gate = await openWork();
|
|
227
|
+
if ("refused" in gate) return gate.refused;
|
|
228
|
+
const { reason, detail } = input as { reason: string; detail?: string };
|
|
229
|
+
const opened = await works.open(gate.id);
|
|
230
|
+
try {
|
|
231
|
+
await opened.append({ type: "work.failed", payload: { reason, detail: detail ?? "" } });
|
|
232
|
+
} finally {
|
|
233
|
+
await opened.close();
|
|
234
|
+
}
|
|
235
|
+
session.clear();
|
|
236
|
+
return json(await works.get(gate.id));
|
|
237
|
+
}
|
|
238
|
+
default: {
|
|
239
|
+
const gate = await openWork();
|
|
240
|
+
if ("refused" in gate) return gate.refused;
|
|
241
|
+
const opened = await works.open(gate.id);
|
|
242
|
+
try {
|
|
243
|
+
const result = await callTool(opened, { id: newCallId(), name, input });
|
|
244
|
+
return toMcpResult(result);
|
|
245
|
+
} finally {
|
|
246
|
+
await opened.close();
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
server.setRequestHandler(ListToolsRequestSchema, async () => ({
|
|
253
|
+
tools: [...WORK_TOOLS, ...registry.list().map((t) => toMcpTool(t.definition))],
|
|
254
|
+
}));
|
|
255
|
+
|
|
256
|
+
server.setRequestHandler(CallToolRequestSchema, (request) =>
|
|
257
|
+
session.run(async () => {
|
|
258
|
+
try {
|
|
259
|
+
return await handle(request.params.name, request.params.arguments ?? {});
|
|
260
|
+
} catch (err) {
|
|
261
|
+
if (isOpenshainError(err)) return failure(`${err.code}: ${err.message}`);
|
|
262
|
+
throw err;
|
|
263
|
+
}
|
|
264
|
+
}),
|
|
265
|
+
);
|
|
266
|
+
|
|
267
|
+
return server;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Records the evidence and the completion. Artifacts named by the agent join the ones the tools
|
|
272
|
+
* wrote; every path must be inside the workspace, and the runtime hashes them all. A path no tool
|
|
273
|
+
* of this work wrote is marked claimed, so a reader can tell the agent's word from the record.
|
|
274
|
+
*/
|
|
275
|
+
async function complete(
|
|
276
|
+
works: WorkStore,
|
|
277
|
+
workspaceRoot: string,
|
|
278
|
+
id: WorkId,
|
|
279
|
+
summary: string,
|
|
280
|
+
claimed: { path: string; sha256?: string }[],
|
|
281
|
+
): Promise<Work> {
|
|
282
|
+
for (const { path } of claimed) await resolveWorkspacePath(workspaceRoot, path);
|
|
283
|
+
const opened = await works.open(id);
|
|
284
|
+
try {
|
|
285
|
+
const events = await opened.events();
|
|
286
|
+
const refs: string[] = [];
|
|
287
|
+
const byPath = new Map<string, string>();
|
|
288
|
+
for (const event of writesWithAfter(events)) {
|
|
289
|
+
refs.push(event.id);
|
|
290
|
+
for (const { path, sha256 } of event.payload.after ?? []) byPath.set(path, sha256);
|
|
291
|
+
}
|
|
292
|
+
const written = new Set(byPath.keys());
|
|
293
|
+
for (const { path, sha256 } of claimed) if (!byPath.has(path)) byPath.set(path, sha256 ?? "");
|
|
294
|
+
const artifacts: Artifact[] = [];
|
|
295
|
+
for (const [path, reported] of byPath) {
|
|
296
|
+
const artifact = await verifyArtifact(workspaceRoot, path, reported);
|
|
297
|
+
artifacts.push(written.has(path) ? artifact : { ...artifact, claimed: true });
|
|
298
|
+
}
|
|
299
|
+
await opened.append({
|
|
300
|
+
type: "evidence.recorded",
|
|
301
|
+
payload: { claim: summary, refs, artifacts },
|
|
302
|
+
});
|
|
303
|
+
await opened.append({ type: "work.completed", payload: { summary } });
|
|
304
|
+
return opened.current();
|
|
305
|
+
} finally {
|
|
306
|
+
await opened.close();
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
function writesWithAfter(events: AnyEvent[]): Event<"tool.completed">[] {
|
|
311
|
+
return events.filter(
|
|
312
|
+
(e): e is Event<"tool.completed"> =>
|
|
313
|
+
e.type === "tool.completed" &&
|
|
314
|
+
!(e as Event<"tool.completed">).payload.isError &&
|
|
315
|
+
(e as Event<"tool.completed">).payload.after !== undefined,
|
|
316
|
+
);
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
function toMcpTool(definition: ToolDefinition): Tool {
|
|
320
|
+
return {
|
|
321
|
+
name: definition.name,
|
|
322
|
+
description: definition.description,
|
|
323
|
+
inputSchema: definition.inputSchema as Tool["inputSchema"],
|
|
324
|
+
};
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
function toMcpResult(result: ToolResult): CallToolResult {
|
|
328
|
+
return {
|
|
329
|
+
content: result.content.map((part) =>
|
|
330
|
+
part.type === "text"
|
|
331
|
+
? { type: "text", text: part.text }
|
|
332
|
+
: { type: "text", text: JSON.stringify(part.value) },
|
|
333
|
+
),
|
|
334
|
+
...(result.isError && { isError: true }),
|
|
335
|
+
};
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
function json(value: unknown): CallToolResult {
|
|
339
|
+
return { content: [{ type: "text", text: JSON.stringify(value) }] };
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
function failure(text: string): CallToolResult {
|
|
343
|
+
return { content: [{ type: "text", text }], isError: true };
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
function newCallId(): string {
|
|
347
|
+
return `call_${Bun.randomUUIDv7()}`;
|
|
348
|
+
}
|
package/src/session.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { WorkId } from "@openshain/core";
|
|
2
|
+
|
|
3
|
+
/** What one connection remembers: the work the agent is on, and the order of its calls. */
|
|
4
|
+
export class Session {
|
|
5
|
+
private currentId: WorkId | undefined;
|
|
6
|
+
private queue: Promise<unknown> = Promise.resolve();
|
|
7
|
+
|
|
8
|
+
get current(): WorkId | undefined {
|
|
9
|
+
return this.currentId;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
select(id: WorkId): void {
|
|
13
|
+
this.currentId = id;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
clear(): void {
|
|
17
|
+
this.currentId = undefined;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** Runs one call after the previous one finished, so calls the agent makes in parallel do not fight over the work's lock. */
|
|
21
|
+
run<T>(fn: () => Promise<T>): Promise<T> {
|
|
22
|
+
const next = this.queue.then(fn, fn);
|
|
23
|
+
this.queue = next.catch(() => undefined);
|
|
24
|
+
return next;
|
|
25
|
+
}
|
|
26
|
+
}
|