MCPcopy
hub / github.com/langchain-ai/langchainjs / tool

Function tool

libs/langchain-core/src/tools/index.ts:888–1025  ·  view source on GitHub ↗
(
  func: (
    input: SchemaOutputT,
    runtime: ToolRuntime<TState, TContext>
  ) => ToolOutputT | Promise<ToolOutputT>,
  fields: ToolWrapperParams<SchemaT, NameT>
)

Source from the content-addressed store, hash-verified

886 | DynamicTool<ToolOutputT>;
887
888export function tool<
889 SchemaT extends InteropZodObject | InteropZodType<string> | JSONSchema =
890 InteropZodObject,
891 NameT extends string = string,
892 SchemaOutputT = ToolInputSchemaOutputType<SchemaT>,
893 SchemaInputT = ToolInputSchemaInputType<SchemaT>,
894 ToolOutputT = ToolOutputType,
895 ToolEventT = ToolEventType,
896 TState = unknown,
897 TContext = unknown,
898>(
899 func: (
900 input: SchemaOutputT,
901 runtime: ToolRuntime<TState, TContext>
902 ) => ToolOutputT | Promise<ToolOutputT>,
903 fields: ToolWrapperParams<SchemaT, NameT>
904):
905 | DynamicStructuredTool<
906 SchemaT,
907 SchemaOutputT,
908 SchemaInputT,
909 ToolOutputT,
910 ToolEventT,
911 NameT
912 >
913 | DynamicTool<ToolOutputT, ToolEventT> {
914 const isSimpleStringSchema = isSimpleStringZodSchema(fields.schema);
915 const isStringJSONSchema = validatesOnlyStrings(fields.schema);
916
917 // If the schema is not provided, or it's a simple string schema, create a DynamicTool
918 if (!fields.schema || isSimpleStringSchema || isStringJSONSchema) {
919 return new DynamicTool<ToolOutputT, ToolEventT>({
920 ...fields,
921 description:
922 fields.description ??
923 (fields.schema as { description?: string } | undefined)?.description ??
924 `${fields.name} tool`,
925 func: async (input, runManager, config) => {
926 return new Promise<ToolOutputT>((resolve, reject) => {
927 const childConfig = patchConfig(config, {
928 callbacks: runManager?.getChild(),
929 });
930 // oxlint-disable-next-line no-void
931 void AsyncLocalStorageProviderSingleton.runWithConfig(
932 pickRunnableConfigKeys(childConfig),
933 async () => {
934 try {
935 // oxlint-disable-next-line @typescript-eslint/no-explicit-any
936 resolve(func(input as any, childConfig as any));
937 } catch (e) {
938 reject(e);
939 }
940 }
941 );
942 });
943 },
944 });
945 }

Callers 15

test.tsFile · 0.90
test.tsFile · 0.90
test.tsFile · 0.90
testParallelToolCallingFunction · 0.90
index.int.test.tsFile · 0.90
applyPatchFunction · 0.90
localShellFunction · 0.90
shellFunction · 0.90
computerUseFunction · 0.90
index.test.tsFile · 0.90

Calls 10

validatesOnlyStringsFunction · 0.90
isSimpleStringZodSchemaFunction · 0.85
patchConfigFunction · 0.85
pickRunnableConfigKeysFunction · 0.85
cleanupFunction · 0.85
getAbortSignalErrorFunction · 0.85
isAsyncGeneratorFunction · 0.85
runWithConfigMethod · 0.80
funcFunction · 0.50
getChildMethod · 0.45

Tested by 7

makeSupervisorFunction · 0.72
makeNestedSupervisorFunction · 0.72
makeParallelSupervisorFunction · 0.72
createToolsFunction · 0.72
makePollToolFunction · 0.72
makeToolFunction · 0.72