Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

DeepSeek Harness in Action: Writing a Replaceable Capability

DeepSeek Harness in Action: Writing a Replaceable Capability

The previous chapter explained the concept of the three roles; in this article we will write it into code following the official tutorial.

We want to implement a capability called myCap: input a piece of text and output it all in uppercase. It is small enough to understand at a glance, yet fully covers the three packages Definition, Provider, and Consumer.

Three steps: Service Definition (abstract class + types) -> Service Provider (implementation subclass) -> Consumer (defineTool).

Finally, load the Provider and Consumer together in cordis.yml.


Step 1: Writing the Service Definition

The Service Definition declares the capability itself: what the service is called, how it is invoked, and what the types of the request and result are.

It contains no implementation logic, only one abstract method and two interfaces.

The abstract class MyCapService inherits from Service and is registered as a named service via super(ctx, 'myCap').

declare module declaration merging gives ctx.myCap its type, a technique explained in article 14.

Example

// File path: packages/my-cap/my-cap/src/index.ts

import

{

Service

,

type Context

}

from

'@deepseek-ai/cordis'

// Declaration merging: gives ctx.myCap type hints in TypeScript

declare module

'@deepseek-ai/cordis'

{

 

interface

Context

{

    myCap

:

MyCapService

 

}

}

// Abstract class: the Definition package only declares the contract, not the implementation

export

abstract

class

MyCapService

extends

Service

{

  constructor

(

ctx

:

Context

)

{

   

super

(

ctx

,

'myCap'

)

// Register as a named service ctx.myCap

 

}

 

/** Execute the capability. */

 

abstract

execute

(

request

:

MyCapRequest

)

:

Promise

<

MyCapResult

}

// Request type: the caller must provide input

export

interface

MyCapRequest

{

  input

:

string

}

// Result type: the capability returns output

export

interface

MyCapResult

{

  output

:

string

}

abstract execute is the only abstract method, which the Provider must implement.

MyCapRequest has the input field, and MyCapResult has the output field.

The request and result types are owned by the Definition package, and both the Provider and the Consumer import from it.


Step 2: Writing the Service Provider

The Service Provider inherits the abstract class and fills in the real behavior.

MyCapLocal converts input to uppercase and returns it.

export const name declares the plugin name, and in apply the service implementation is registered via ctx.plugin(MyCapLocal).

Example

// File path: packages/my-cap/my-cap-local/src/index.ts

import

type

{

Context

}

from

'@deepseek-ai/cordis'

import

{

MyCapService

,

type MyCapRequest

,

type MyCapResult

}

from

'@deepseek-ai/dsh-my-cap'

// Implementation class: depends only on the Definition package

class

MyCapLocal

extends

MyCapService

{

  async execute

(

request

:

MyCapRequest

)

:

Promise

<

MyCapResult

{

// Local provider behavior.

   

return

{

output

:

request.

input

.

toUpperCase

(

)

}

 

}

}

export

const

name

=

'my-cap-local'

export

function

apply

(

ctx

:

Context

)

{

 

// Load the implementation class as a plugin, registering it as the actual service of ctx.myCap

  ctx.

plugin

(

MyCapLocal

)

}

The Provider depends only on the abstract classes and types in the Definition package.

It does not care how the model invokes it, nor what the tool looks like.

If you want to switch to a different implementation in the future, such as executing on a remote server, you only need to write another Provider subclass.


Step 3: Writing the Consumer

The Consumer wraps the capability into a tool that the model can call.

inject declares dependencies on tools and myCap, ensuring that apply is executed only after both services are ready.

defineTool declares the tool schema; when the model sees the my_cap tool it will pass arguments according to parameters.

In execute, call ctx.myCap.execute and return the result as the tool output.

Example

// File path: packages/my-cap/tool-my-cap/src/index.ts

import

type

{

Context

}

from

'@deepseek-ai/cordis'

import

{

defineTool

}

from

'@deepseek-ai/dsh-tools'

export

const

name

=

'tool-my-cap'

// Dependency declaration: tools provides the registration entry, myCap provides the service implementation

export

const

inject

=

[

'tools'

,

'myCap'

]

export

function

apply

(

ctx

:

Context

)

{

  ctx.

tools

.

register

(

defineTool

(

{

    name

:

'my_cap'

,

// The model-facing tool name

    description

:

'Execute my capability.'

,

   

// Parameter schema: the model generates parameters according to it

    parameters

:

{

      input

:

{

type

:

'string'

,

required

:

true

}

,

   

}

,

   

// Output schema and rendering method

    output

:

{

      schema

:

{

type

:

'string'

}

,

      render

:

(

_args

,

value

)

=>

[

{

type

:

'text'

,

text

:

value

}

]

,

   

}

,

   

// Actual execution: call the capability service

    async execute

(

args

)

{

     

const

result

=

await ctx.

myCap

.

execute

(

{

input

:

args.

input

}

)

     

return

result.

output

   

}

,

 

}

)

)

}

The model does not directly know ctx.myCap; it only knows the my_cap tool.

The Consumer is the bridge between the two and is also responsible for rendering the result into text visible to the model.


myCap three-role structure and data flow

Putting the three packages together, data starts from the model, flows through the Consumer and Definition, and lands on the Provider.

myCap three-role structure and data flow diagram

The model first calls the my_cap tool; this step happens only in the Consumer layer.

The Consumer wraps the parameters into a MyCapRequest and passes it to ctx.myCap.execute(request).

Definition delegates the call to the currently loaded Provider implementation.

After the Provider computes the result, it returns the MyCapResult to the Consumer along the same path.

The Consumer renders output into text and hands it to the model as a tool result.

Throughout the whole chain, when the Provider is swapped out, neither the Consumer nor the model notices.


Combining in cordis.yml

Writing the three packages only produces parts; you also need to load the Provider and Consumer together in cordis.yml.

Load the Provider so ctx.myCap has an implementation; load the Consumer so the model has the my_cap tool.

Example

File path: cordis.yml

Load the Provider first so ctx.myCap has an implementation

:

'@deepseek-ai/dsh-my-cap-local'

Then load the Consumer so the model can use the my_cap tool

:

'@deepseek-ai/dsh-tool-my-cap'

As for loading order, Cordis sorts automatically by dependency, so it does not matter whether the Provider or Consumer comes first.

To switch to a different Provider, just change the first line; the Consumer stays unchanged.


Design points

The official docs give three design points; it is best to remember them before writing a capability.

First, do not split preemptively.

Only put roles into different packages when they need to evolve independently.

A simple tool plugin needs no split; it is perfectly fine for one package to take on multiple roles.

Second, the Service Definition owns the Request/Result types.

The Provider and Consumer only depend on the Definition package, so the request and result types must be defined by the Definition.

Third, explicit is better than implicit.

The implementation should handle defaults through an explicit resolve(request) step, rather than hiding ?? default inside run().

The Bash seam is an example: the workdir and timeoutMs of ShellExecRequest are optional; the tool layer first calls ctx.shell.resolve(request) to get a ShellExecSpec with all fields required, then hands it to run().

Do not silently fill in defaults inside run(); resolve first, then execute, so the boundary is clear.


Summary and self-test

In one sentence: writing a replaceable capability = Definition abstract class + Provider subclass + Consumer tool, plus a cordis.yml.

Self-test question 1: In which package should MyCapRequest and MyCapResult be defined?

Self-test question 2: Is there any dependency between the Provider and Consumer?

Self-test question 3: Why is preemptive splitting not recommended for a simple tool plugin?